Best JavaScript code snippet using wpt
supersyntax.js
Source:supersyntax.js  
1//-------------------------------------------------------------------------------------------------------2// Copyright (C) Microsoft. All rights reserved.3// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.4//-------------------------------------------------------------------------------------------------------5// Using super is invalid in many places.6// These tests are generated and are intended to use to verify the restriction is done properly in various contexts7// Here is the C# code used the generate these test cases8/*9namespace ManagedWorkspace10{11    using System;12    using System.Collections.Generic;13    // state = 0 -> not allowed14    // state = 1 -> all allowed15    // state = 2 -> only member allowed16    internal static class Program17    {18        private static void Main(string[] args)19        {20            LeafNode n = new LeafNode();21            BranchNode rootNode = new BranchNode();22            BranchNode childNode = new BranchNode();23            BranchOut(rootNode, n);24            BranchOut(rootNode, childNode);25            BranchOut(childNode, n);26            int testCaseNumber = 1;27            foreach (var code in rootNode.Generate(0))28            {29                Console.WriteLine(GenerateTest(code, testCaseNumber));30                testCaseNumber++;31            }32        }33        private static string GenerateTest(Tuple<string, bool> testCase, int testCaseNumber)34        {35            string goodFormatString = @"assert.doesNotThrow(function () {{ WScript.LoadScript("","samethread").WScript.LoadScript('{0}'); }}, ""generated test #{1}"");";36            string badFormatString = @"assert.throws(() => {{ WScript.LoadScript("","samethread").WScript.LoadScript('{0}'); }}, SyntaxError, ""generatedTest #{1}"", ""Invalid use of the 'super' keyword"");";37            if (testCase.Item2)38            {39                return string.Format(goodFormatString, testCase.Item1, testCaseNumber);40            }41            else42            {43                return string.Format(badFormatString, testCase.Item1, testCaseNumber);44            }45        }46        private static void BranchOut(BranchNode rootNode, Node n)47        {48            rootNode.branches.Add(new Branch("class X { constructor(){+} }", n, (oldState) => 1));49            rootNode.branches.Add(new Branch("class X { x(){+} }", n, (oldState) => 2));50            rootNode.branches.Add(new Branch("function x(){+}", n, (oldState) => 0));51            rootNode.branches.Add(new Branch("() => {+}", n, (oldState) => oldState));52        }53    }54    class Branch55    {56        public Branch(string userFormatString, Node branchTarget, Func<int, int> statePropagator)57        {58            this.BranchTarget = branchTarget;59            this.FormatString = ToFormatString(userFormatString);60            this.StatePropagator = statePropagator;61        }62        public Node BranchTarget { get; set; }63        public string FormatString { get; set; }64        public Func<int, int> StatePropagator { get; set; }65        private string ToFormatString(string p)66        {67            return p.Replace("{", "{{").Replace("}", "}}").Replace("+", "{0}");68        }69    }70    abstract class Node71    {72        public abstract IEnumerable<Tuple<string, bool>> Generate(int state);73    }74    class LeafNode : Node75    {76        public override IEnumerable<Tuple<string, bool>> Generate(int state)77        {78            yield return Tuple.Create("super();", state == 1);79            yield return Tuple.Create("super.x;", state > 0);80        }81    }82    class BranchNode : Node83    {84        public List<Branch> branches = new List<Branch>();85        public override IEnumerable<Tuple<string, bool>> Generate(int state)86        {87            foreach (var branch in branches)88            {89                Node branchTarget = branch.BranchTarget;90                string branchFormatString = branch.FormatString;91                foreach (var childItem in branchTarget.Generate(branch.StatePropagator(state)))92                {93                    string childString = childItem.Item1;94                    bool childResult = childItem.Item2;95                    yield return Tuple.Create(string.Format(branchFormatString, childString), childResult);96                }97            }98        }99    }100}101 */102WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");103var tests = [104    {105        name: "Generated tests",106        body: function () {107            assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { constructor(){super();} }'); }, "generated test #1");108            assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { constructor(){super.x;} }'); }, "generated test #2");109            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('class X { x(){super();} }'); }, SyntaxError, "generatedTest #3", "Invalid use of the 'super' keyword: generatedTest #3");110            assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { x(){super.x;} }'); }, "generated test #4");111            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('function x(){super();}'); }, SyntaxError, "generatedTest #5", "Invalid use of the 'super' keyword");112            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('function x(){super.x;}'); }, SyntaxError, "generatedTest #6", "Invalid use of the 'super' keyword");113            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('() => {super();}'); }, SyntaxError, "generatedTest #7", "Invalid use of the 'super' keyword");114            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('() => {super.x;}'); }, SyntaxError, "generatedTest #8", "Invalid use of the 'super' keyword");115            assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { constructor(){class X { constructor(){super();} }} }'); }, "generated test #9");116            assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { constructor(){class X { constructor(){super.x;} }} }'); }, "generated test #10");117            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('class X { constructor(){class X { x(){super();} }} }'); }, SyntaxError, "generatedTest #11", "Invalid use of the 'super' keyword");118            assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { constructor(){class X { x(){super.x;} }} }'); }, "generated test #12");119            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('class X { constructor(){function x(){super();}} }'); }, SyntaxError, "generatedTest #13", "Invalid use of the 'super' keyword");120            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('class X { constructor(){function x(){super.x;}} }'); }, SyntaxError, "generatedTest #14", "Invalid use of the 'super' keyword");121            assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { constructor(){() => {super();}} }'); }, "generated test #15");122            assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { constructor(){() => {super.x;}} }'); }, "generated test #16");123            assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { x(){class X { constructor(){super();} }} }'); }, "generated test #17");124            assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { x(){class X { constructor(){super.x;} }} }'); }, "generated test #18");125            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('class X { x(){class X { x(){super();} }} }'); }, SyntaxError, "generatedTest #19", "Invalid use of the 'super' keyword");126            assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { x(){class X { x(){super.x;} }} }'); }, "generated test #20");127            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('class X { x(){function x(){super();}} }'); }, SyntaxError, "generatedTest #21", "Invalid use of the 'super' keyword");128            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('class X { x(){function x(){super.x;}} }'); }, SyntaxError, "generatedTest #22", "Invalid use of the 'super' keyword");129            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('class X { x(){() => {super();}} }'); }, SyntaxError, "generatedTest #23", "Invalid use of the 'super' keyword");130            assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('class X { x(){() => {super.x;}} }'); }, "generated test #24");131            assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('function x(){class X { constructor(){super();} }}'); }, "generated test #25");132            assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('function x(){class X { constructor(){super.x;} }}'); }, "generated test #26");133            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('function x(){class X { x(){super();} }}'); }, SyntaxError, "generatedTest #27", "Invalid use of the 'super' keyword");134            assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('function x(){class X { x(){super.x;} }}'); }, "generated test #28");135            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('function x(){function x(){super();}}'); }, SyntaxError, "generatedTest #29", "Invalid use of the 'super' keyword");136            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('function x(){function x(){super.x;}}'); }, SyntaxError, "generatedTest #30", "Invalid use of the 'super' keyword");137            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('function x(){() => {super();}}'); }, SyntaxError, "generatedTest #31", "Invalid use of the 'super' keyword");138            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('function x(){() => {super.x;}}'); }, SyntaxError, "generatedTest #32", "Invalid use of the 'super' keyword");139            assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('() => {class X { constructor(){super();} }}'); }, "generated test #33");140            assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('() => {class X { constructor(){super.x;} }}'); }, "generated test #34");141            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('() => {class X { x(){super();} }}'); }, SyntaxError, "generatedTest #35", "Invalid use of the 'super' keyword");142            assert.doesNotThrow(function () { WScript.LoadScript("","samethread").WScript.LoadScript('() => {class X { x(){super.x;} }}'); }, "generated test #36");143            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('() => {function x(){super();}}'); }, SyntaxError, "generatedTest #37", "Invalid use of the 'super' keyword");144            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('() => {function x(){super.x;}}'); }, SyntaxError, "generatedTest #38", "Invalid use of the 'super' keyword");145            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('() => {() => {super();}}'); }, SyntaxError, "generatedTest #39", "Invalid use of the 'super' keyword");146            assert.throws(() => { WScript.LoadScript("","samethread").WScript.LoadScript('() => {() => {super.x;}}'); }, SyntaxError, "generatedTest #40", "Invalid use of the 'super' keyword");147        }148    }149];...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
