Chromium Code Reviews| Index: tests/SkSLErrorTest.cpp |
| diff --git a/tests/SkSLErrorTest.cpp b/tests/SkSLErrorTest.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..daf6823dd45e36c3c6f6d89763a2387cc2fe0c23 |
| --- /dev/null |
| +++ b/tests/SkSLErrorTest.cpp |
| @@ -0,0 +1,331 @@ |
| +/* |
| + * Copyright 2016 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#include "SkSLCompiler.h" |
| + |
| +#include "Test.h" |
| + |
| +DEF_TEST(SkSLUndefinedSymbol, r) { |
| + SkSL::Compiler compiler; |
|
dogben
2016/06/20 18:26:26
nit: Given that these tests are very similar, putt
ethannicholas
2016/06/20 20:17:10
Crap, I had meant to go do that. Fixed.
|
| + std::stringstream out; |
| + compiler.errorText(); |
|
dogben
2016/06/20 18:26:26
unused?
ethannicholas
2016/06/20 20:17:10
Once upon a time, calling errorText() returned the
|
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { x = vec2(1); }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: unknown identifier 'x'\n1 error\n")); |
|
dogben
2016/06/20 18:26:26
nit: == can compare std::string and char*, so "std
|
| +} |
| + |
| +DEF_TEST(SkSLUndefinedFunction, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { int x = foo(1); }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: unknown identifier 'foo'\n1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLGenericParameterMismatch, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { float x = sin(1, 2); }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: no match for sin(int, int)\n1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLParameterCountMismatch, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "float foo(float x) { return x * x; }" |
| + "void main() { float x = foo(1, 2); }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: call to 'foo' expected 1 parameter, but found 2\n" |
|
dogben
2016/06/20 18:26:26
nit: maybe add \n to the code to test the line num
|
| + "1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLParameterMismatch, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "float foo(float x) { return x * x; }" |
| + "void main() { float x = foo(true); }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: expected 'float', but found 'bool'\n1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLIfTypeMismatch, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { if (3) { } }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: expected 'bool', but found 'int'\n1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLDoTypeMismatch, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { do { } while (vec2(1)); }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: expected 'bool', but found 'vec2'\n1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLWhileTypeMismatch, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { while (vec3(1)) { } }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: expected 'bool', but found 'vec3'\n1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLForTypeMismatch, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { for (int x = 0; x; x++) { } }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: expected 'bool', but found 'int'\n1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLConstructorTypeMismatch, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { vec2 x = vec2(1.0, false); }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: expected 'float', but found 'bool'\n1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLConstructorParameterCount, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { vec3 x = vec3(1.0, 2.0); }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: invalid parameters to 'vec3' constructor (expected 3 " |
| + "scalars, but found 2)\n1 error\n")); |
| + |
| + result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { vec3 x = vec3(1.0, 2.0, 3.0, 4.0); }", |
| + out); |
| + REPORTER_ASSERT(r, result); |
|
dogben
2016/06/20 18:26:26
There is no error when constructing vec3 with 4 va
ethannicholas
2016/06/20 20:17:10
Stupid, huh? For whatever reason, it's supposed to
|
| +} |
| + |
| +DEF_TEST(SkSLSwizzleScalar, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { float x = 1; float y = x.y; }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: cannot swizzle value of type 'float'\n1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLSwizzleMatrix, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { mat2 x = mat2(1); float y = x.y; }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: cannot swizzle value of type 'mat2'\n1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLSwizzleOutOfBounds, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { vec3 test = vec2(1).xyz; }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: invalid swizzle component 'z'\n1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLSwizzleTooManyComponents, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { vec4 test = vec2(1).xxxxx; }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: too many components in swizzle mask 'xxxxx'\n" |
| + "1 error\n")); |
| +} |
| +DEF_TEST(SkSLMissingSemicolon, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { float x = 1 float y = 2; }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: expected ';', but found 'float'\n1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLAssignmentTypeMismatch, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { int x = 1.0; }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: expected 'int', but found 'float'\n1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLReturnFromVoid, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { return true; }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: may not return a value from a void function\n1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLReturnMissingValue, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "int foo() { return; } void main() { }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: expected function to return 'int'\n1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLReturnTypeMismatch, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "int foo() { return 1.0; } void main() { }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: expected 'int', but found 'float'\n1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLDuplicateFunction, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { } void main() { }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: duplicate definition of void main()\n1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLDifferentReturnType, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "int main() { } void main() { }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: functions 'void main()' and 'int main()' differ only " |
| + "in return type\n1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLDuplicateSymbol, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "int main; void main() { }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: symbol 'main' was already defined\n1 error\n")); |
| + |
| + result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "int x; int x; void main() { }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: symbol 'x' was already defined\n1 error\n")); |
| + |
| + result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "int x; void main() { int x; }", |
| + out); |
| + REPORTER_ASSERT(r, result); |
| +} |
| + |
| +DEF_TEST(SkSLBinaryTypeMismatch, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { float x = 3 * true; }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n" |
| + "1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLCallNonFunction, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { float x = 3; x(); }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: 'x' is not a function\n1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLInvalidUnary, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { mat4 x = mat4(1); x++; }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: '++' cannot operate on 'mat4'\n1 error\n")); |
| +} |
| + |
| +DEF_TEST(SkSLInvalidAssignment, r) { |
| + SkSL::Compiler compiler; |
| + std::stringstream out; |
| + bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, |
| + "void main() { 1 = 2; }", |
| + out); |
| + REPORTER_ASSERT(r, !result); |
| + REPORTER_ASSERT(r, compiler.errorText() == |
| + std::string("error: 1: cannot assign to '1'\n1 error\n")); |
| +} |