<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>WebGL Shader Conformance Tests</title>
<link rel="stylesheet" href="../../../../resources/js-test-style.css"/>
<script src="../../../../js/js-test-pre.js"></script>
<script src="../../../../js/webgl-test.js"></script>
<script src="../../../../js/webgl-test-utils.js"></script>
<script src="shader-utils.js"></script>
<script src="shader-library.js"></script>
<script src="shader-library-case.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<script>

        description("Testing the .test parser components.");

        var wtu                = WebGLTestUtils;
        var successfullyParsed = false;
        var parser             = new shaderLibrary.Parser();


        // this array of functions is processed sequentially.
        // returning false will abort
        var tests = [


                function() {
                        var result = shaderLibrary.isWhitespace(" ");
                        test_result(
                                "Checking isWhitespace. Passing \" \". Expecting true",
                                result === true, result
                        );
                        return true;
                },
                function() {
                        var result = shaderLibrary.isWhitespace("q");
                        test_result(
                                "Checking isWhitespace. Passing \"q\". Expecting false",
                                result === false, result
                        );
                        return true;
                },
                function() {
                        var result = shaderLibrary.isWhitespace(" \r\n\t");
                        test_result(
                                "Checking isWhitespace. Passing \" \\r\\n\\t\". Expecting true",
                                result === true, result
                        );
                        return true;
                },
                function() {
                        var result = shaderLibrary.isWhitespace(" \r\n\tq");
                        test_result(
                                "Checking isWhitespace. Passing \" \\r\\n\\tq\". Expecting false",
                                result === false, result
                        );
                        return true;
                },

                function() {
                        try {
                                var result = shaderLibrary.removeExtraIndentation(
                                        "\t\t\taaaa\n\t    \t\tbbbb\n\t\t\tcccc\r\n\t\t\tdddd"
                                );
                        } catch (err) {
                                result = "aborted";
                        }

                        test_result(
                                "Checking removeExtraIndentation. \
                                Passing \"\t\t\taaaa\n\t    \t\tbbbb\n\t\t\tcccc\r\n\t\t\tdddd\". \
                                Expecting \"aaaa\n\tbbbb\ncccc\ndddd\"",
                                result === "aaaa\n\tbbbb\ncccc\ndddd",
                                result
                        );
                        return true;
                },


                function() {
                        // looking for private access to the parser object
                        var result = (typeof parser.priv != 'undefined');
                        test_result("looking for private member access", result, result);
                        return result;
                },

                function() {
                        try {
                                var result = parser.priv.parseIntLiteral("42");
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking int parser. Passing \"42\". Expecting 42",
                                result === 42, result
                        );
                        return true;
                },
                function() {
                        try {
                                var result = parser.priv.parseIntLiteral("-5");
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking int parser. Passing \"-5\". Expecting -5",
                                result === -5, result
                        );
                        return true;
                },
                function() {
                        try {
                                var result = parser.priv.parseIntLiteral("42.9");
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking int parser. Passing \"42.9\". Expecting 42",
                                result === 42, result
                        );
                        return true;
                },
                function() {
                        try {
                                var result = parser.priv.parseFloatLiteral("42.9");
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking float parser. Passing \"42.9\". Expecting 42.9",
                                result === 42.9, result
                        );
                        return true;
                },


                function() {
                        try {
                                var result = parser.priv.parseStringLiteral("\"hello world\"");
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking string parser. Passing \"\"hello world\"\". Expecting \"hello world\"",
                                result == "hello world", result
                        );
                        return true;
                },
                function() {
                        try {
                                var result = parser.priv.parseStringLiteral("\"hello world\"extra");
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking string parser. Passing \"\"hello world\"extra\". Expecting \"hello world\"",
                                result == "hello world", result
                        );
                        return true;
                },
                function() {
                        try {
                                var result = parser.priv.parseStringLiteral("\"\\\"\\\"\\\"\"\\\"\"");
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking string parser. Passing \"\"\\\"\\\"\\\"\"\\\"\"\". Expecting \"\"\"\"\"",
                                result == "\"\"\"", result
                        );
                        return true;
                },
                function() {
                        try {
                                var result = parser.priv.parseStringLiteral("\"newline test\\nnewline test\"");
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking string parser. Passing \"\"newline test\\nnewline test\"\". Expecting \"newline test\nnewline test\"",
                                result == "newline test\nnewline test", result
                        );
                        return true;
                },
                function() {
                        try {
                                var result = parser.priv.parseStringLiteral("\"tab test\\ttab test\"");
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking string parser. Passing \"\"tab test\\ttab test\"\". Expecting \"tab test\ttab test\"",
                                result == "tab test\ttab test", result
                        );
                        return true;
                },

                function() {
                        try {
                                var result = parser.priv.parseShaderSource(
                                        "\"\"\t\t\taaaa\n\t    \t\tbbbb\n\t\t\tcccc\r\n\t\t\tdddd\"\""
                                );
                        } catch (err) {
                                result = "aborted";
                        }

                        test_result(
                                "Checking parseShaderSource. \
                                Passing \"\"\"\t\t\taaaa\n\t    \t\tbbbb\n\t\t\tcccc\r\n\t\t\tdddd\"\"\". \
                                Expecting \"aaaa\n\tbbbb\ncccc\ndddd\"",
                                result === "aaaa\n\tbbbb\ncccc\ndddd",
                                result
                        );
                        return true;
                },

                function() {
                        try {
                                var result = parser.priv.advanceTokenTester("bool value = true;", 0);
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking advanceToken. Passing \"bool value = true;\", 0",
                                result.idType == parser.priv.Token.TOKEN_BOOL && result.value == "bool",
                                result.name + ": " + result.value
                        );
                },

                function() {
                        try {
                                var result = parser.priv.advanceTokenTester("bool value = true;", 4);
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking advanceToken. Passing \"bool value = true;\", 4",
                                result.idType == parser.priv.Token.TOKEN_IDENTIFIER && result.value == "value",
                                result.name + ": " + result.value
                        );
                },
                //*
                function() {
                        try {
                                var result = parser.priv.advanceTokenTester("bool value = true;", 11);
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking advanceToken. Passing \"bool value = true;\", 11",
                                result.idType == parser.priv.Token.TOKEN_ASSIGN,
                                result.name + ": " + result.value
                        );
                },
                //*/
                function() {
                        try {
                                var result = parser.priv.advanceTokenTester("bool value = true;", 12);
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking advanceToken. Passing \"bool value = true;\", 12",
                                result.idType == parser.priv.Token.TOKEN_TRUE && result.value == "true",
                                result.name + ": " + result.value
                        );
                },
                function() {
                        try {
                                var result = parser.priv.advanceTokenTester("bool value = true;", 17);
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking advanceToken. Passing \"bool value = true;\", 17",
                                result.idType == parser.priv.Token.TOKEN_SEMI_COLON && result.value == ";",
                                result.name + ": " + result.value
                        );
                },
                function() {
                        try {
                                var result = parser.priv.advanceTokenTester("-7", 0);
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking advanceToken. Passing \"-7\", 0",
                                result.idType == parser.priv.Token.TOKEN_MINUS && result.value == "-",
                                result.name + ": " + result.value
                        );
                },
                function() {
                        try {
                                var result = parser.priv.advanceTokenTester("-7", 1);
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking advanceToken. Passing \"-7\", 1",
                                result.idType == parser.priv.Token.TOKEN_INT_LITERAL && result.value == "7",
                                result.name + ": " + result.value
                        );
                },
                function() {
                        try {
                                var result = parser.priv.advanceTokenTester("3.145", 0);
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking advanceToken. Passing \"3.145\", 0",
                                result.idType == parser.priv.Token.TOKEN_FLOAT_LITERAL && result.value == "3.145",
                                result.name + ": " + result.value
                        );
                },
                function() {
                        try {
                                var result = parser.priv.advanceTokenTester("1.0e+10", 0);
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking advanceToken. Passing \"1.0e+10\", 0",
                                result.idType == parser.priv.Token.TOKEN_FLOAT_LITERAL && result.value == "1.0e+10",
                                result.name + ": " + result.value
                        );
                },
                function() {
                        try {
                                var result = parser.priv.advanceTokenTester("1.0e", 0);
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking advanceToken. Passing \"1.0e\", 0",
                                result === "aborted",
                                result
                        );
                },
                function() {
                        try {
                                var result = parser.priv.advanceTokenTester("\"\"shader source\"\";", 0);
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking advanceToken. Passing \"\"\"shader source\"\";\", 0",
                                result.idType == parser.priv.Token.TOKEN_SHADER_SOURCE && result.value == "\"\"shader source\"\"",
                                result.name + ": " + result.value
                        );
                },
                function() {
                        try {
                                var result = parser.priv.advanceTokenTester("\"string\";", 0);
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking advanceToken. Passing \"\"string\";\", 0",
                                result.idType == parser.priv.Token.TOKEN_STRING && result.value == "\"string\"",
                                result.name + ": " + result.value
                        );
                },
                function() {
                        try {
                                var result = parser.priv.advanceTokenTester("\'string\';", 0);
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking advanceToken. Passing \"\'string\';\", 0",
                                result.idType == parser.priv.Token.TOKEN_STRING && result.value == "\'string\'",
                                result.name + ": " + result.value
                        );
                },
                function() {
                        try {
                                var result = parser.priv.advanceTokenTester("\'string\";", 0);
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking advanceToken. Passing \"\'string\";\", 0",
                                result === "aborted",
                                result
                        );
                },
                function() {
                        try {
                                var result = parser.priv.advanceTokenTester("\'string\\", 0);
                        } catch (err) {
                                result = "aborted";
                        }
                        test_result(
                                "Checking advanceToken. Passing \"\'string\\\", 0",
                                result === "aborted",
                                result
                        );
                },
                function() {
                        try {
                                var result = parser.priv.mapDataTypeToken(parser.priv.Token.TOKEN_INT);
                        } catch (err) {
                                var result = "aborted";
                        }
                        test_result(
                                "Checking advanceToken. Passing parser.priv.Token.TOKEN_INT",
                                result === shaderUtils.DataType.TYPE_INT,
                                result
                        );
                },

                function() {
                        successfullyParsed = true;i
                        return true;
                },

        ];

        var current = 0;

        function test_result(description, result, additional) {
                switch (typeof(additional)) {
                 case "undefined":
                        break;
                 case "string":
                        description += ": \"" + additional + "\"";
                        break;
                 default:
                        description += ": " + additional;
                        break;
                }
                if (result) {
                        testPassed(description);
                } else {
                        testFailed(description);
                }
        }

        function do_next() {
                if (tests[current]() !== false && tests.length > ++current) {
                        setTimeout(do_next, 0);
                } else {
                        on_complete();
                }
        }

        function on_complete() {
                var testPost = document.createElement('script');
                testPost.src = "../../../../js/js-test-post.js";
                document.body.appendChild(testPost);
        }

        do_next();

//	
</script>
</body>
</html>
