using System; using System.Collections.Generic; using System.Linq; using System.Text; using UnitTestSharpHLSL; namespace UnitTestSharpHLSLTests { public class TestFinderTests : UnitTestSharp.TestFixture { public class Tokenizing : UnitTestSharp.TestFixture { public void Spaces() { string text = " this is a test "; var tokenized = UnitTestSharpHLSL.TestFinder.TokenizeHLSL(text); CheckEqual(4, tokenized.Count); CheckEqual("this", tokenized[0].token); CheckEqual("is", tokenized[1].token); CheckEqual("a", tokenized[2].token); CheckEqual("test", tokenized[3].token); CheckEqual(1, tokenized[0].linenumber); CheckEqual(1, tokenized[1].linenumber); CheckEqual(1, tokenized[2].linenumber); CheckEqual(1, tokenized[3].linenumber); } public void NewLines() { string text = " \n\n \n this \n is \na\n \ntest \n\n\n\n"; var tokenized = UnitTestSharpHLSL.TestFinder.TokenizeHLSL(text); CheckEqual(4, tokenized.Count); CheckEqual("this", tokenized[0].token); CheckEqual("is", tokenized[1].token); CheckEqual("a", tokenized[2].token); CheckEqual("test", tokenized[3].token); CheckEqual(4, tokenized[0].linenumber); CheckEqual(5, tokenized[1].linenumber); CheckEqual(6, tokenized[2].linenumber); CheckEqual(8, tokenized[3].linenumber); } public void Tabs() { string text = " \n\n \n \t\t \tthis\t \n is\t \na\n \ntest \n\n\t\n\n"; var tokenized = UnitTestSharpHLSL.TestFinder.TokenizeHLSL(text); CheckEqual(4, tokenized.Count); CheckEqual("this", tokenized[0].token); CheckEqual("is", tokenized[1].token); CheckEqual("a", tokenized[2].token); CheckEqual("test", tokenized[3].token); CheckEqual(4, tokenized[0].linenumber); CheckEqual(5, tokenized[1].linenumber); CheckEqual(6, tokenized[2].linenumber); CheckEqual(8, tokenized[3].linenumber); } public void Punctuation_1() { string text = "this is(a test)"; var tokenized = UnitTestSharpHLSL.TestFinder.TokenizeHLSL(text); var list = tokenized.ToList(); CheckEqual(6, list.Count); CheckEqual("this", list[0].token); CheckEqual("is", list[1].token); CheckEqual("(", list[2].token); CheckEqual("a", list[3].token); CheckEqual("test", list[4].token); CheckEqual(")", list[5].token); CheckEqual(1, tokenized[0].linenumber); CheckEqual(1, tokenized[1].linenumber); CheckEqual(1, tokenized[2].linenumber); CheckEqual(1, tokenized[3].linenumber); CheckEqual(1, tokenized[4].linenumber); CheckEqual(1, tokenized[5].linenumber); } public void Punctuation_2() { string text = "this is({}a test}})"; string parsed = "this is ( { } a test } } )"; var tokenized = UnitTestSharpHLSL.TestFinder.TokenizeHLSL(text).ToList(); var reference = parsed.Split(' '); CheckEqual(tokenized.Count(), reference.Count()); for (int i = 0; i < tokenized.Count(); ++i) { CheckEqual(reference[i], tokenized[i].token); CheckEqual(1, tokenized[i].linenumber); } } public void EmptyString() { string text = ""; var tokenized = UnitTestSharpHLSL.TestFinder.TokenizeHLSL(text); CheckEqual(0, tokenized.Count()); } public void OnlyWhitespace() { string text = " \n\n\n \t \t \t \r\r\r"; var tokenized = UnitTestSharpHLSL.TestFinder.TokenizeHLSL(text); CheckEqual(0, tokenized.Count()); } public void Underscores() { string text = " this_is_a_single_variable\n\n"; var tokenized = UnitTestSharpHLSL.TestFinder.TokenizeHLSL(text); CheckEqual(1, tokenized.Count()); CheckEqual("this_is_a_single_variable", tokenized.First().token); CheckEqual(1, tokenized.First().linenumber); } public void LineComments() { // comment for comparison string text = " \n\n\n this is\n//Bogus Text\na test//post comment\n\n"; var tokenized = UnitTestSharpHLSL.TestFinder.TokenizeHLSL(text); CheckEqual(4, tokenized.Count); CheckEqual("this", tokenized[0].token); CheckEqual("is", tokenized[1].token); CheckEqual("a", tokenized[2].token); CheckEqual("test", tokenized[3].token); CheckEqual(4, tokenized[0].linenumber); CheckEqual(4, tokenized[1].linenumber); CheckEqual(6, tokenized[2].linenumber); CheckEqual(6, tokenized[3].linenumber); } public void StringLiteral() { string text = @" char *name = ""This is a string literal with \""inner quotes\""""; "; var tokenized = UnitTestSharpHLSL.TestFinder.TokenizeHLSL(text); CheckEqual(6, tokenized.Count); CheckEqual("char", tokenized[0].token); CheckEqual("*", tokenized[1].token); CheckEqual("name", tokenized[2].token); CheckEqual("=", tokenized[3].token); CheckEqual(@"""This is a string literal with \""inner quotes\""""", tokenized[4].token); CheckEqual(";", tokenized[5].token); CheckEqual(1, tokenized[0].linenumber); CheckEqual(1, tokenized[1].linenumber); CheckEqual(1, tokenized[2].linenumber); CheckEqual(1, tokenized[3].linenumber); CheckEqual(1, tokenized[4].linenumber); CheckEqual(1, tokenized[5].linenumber); } /// /// See http://ostermiller.org/findcomment.html /// public class CStyleComments : UnitTestSharp.TestFixture { public void Basic() { string comment = @"/* First comment */"; var tokenized = UnitTestSharpHLSL.TestFinder.TokenizeHLSL(comment); CheckEqual(0, tokenized.Count()); } public void FirstTry() { string comment = @"/* First comment first comment—line two*/ /* Second comment */ "; var tokenized = UnitTestSharpHLSL.TestFinder.TokenizeHLSL(comment); CheckEqual(0, tokenized.Count()); } public void SecondTry() { string comment = @"start_code /* First comment */ more_code /* Second comment */ end_code"; var tokenized = UnitTestSharpHLSL.TestFinder.TokenizeHLSL(comment).ToList(); CheckEqual(3, tokenized.Count); CheckEqual("start_code", tokenized[0].token); CheckEqual("more_code", tokenized[1].token); CheckEqual("end_code", tokenized[2].token); CheckEqual(1, tokenized[0].linenumber); CheckEqual(3, tokenized[1].linenumber); CheckEqual(5, tokenized[2].linenumber); } public void ThirdTry() { string comment = @"/* * Common multi-line comment style. */ /* Second comment */ "; var tokenized = UnitTestSharpHLSL.TestFinder.TokenizeHLSL(comment); CheckEqual(0, tokenized.Count()); } public void FourthTry() { string comment = @"start_code /**** * Common multi-line comment style. ****/ more_code /* * Another common multi-line comment style. */ end_code"; var tokenized = UnitTestSharpHLSL.TestFinder.TokenizeHLSL(comment).ToList(); CheckEqual(3, tokenized.Count); CheckEqual("start_code", tokenized[0].token); CheckEqual("more_code", tokenized[1].token); CheckEqual("end_code", tokenized[2].token); CheckEqual(1, tokenized[0].linenumber); CheckEqual(5, tokenized[1].linenumber); CheckEqual(9, tokenized[2].linenumber); } public void FifthTry() { string comment = @"/**** * Common multi-line comment style. ****/ /**** * Another common multi-line comment style. */ "; var tokenized = UnitTestSharpHLSL.TestFinder.TokenizeHLSL(comment); CheckEqual(0, tokenized.Count()); } public void CommentedOutComment() { string comment = @" // The comment around this code has been commented out. // /* some_code(); // */ "; string parsed = "some_code ( ) ;"; var tokenized = UnitTestSharpHLSL.TestFinder.TokenizeHLSL(comment).ToList(); var reference = parsed.Split(' '); CheckEqual(tokenized.Count(), reference.Count()); for (int i = 0; i < tokenized.Count(); ++i) { CheckEqual(reference[i], tokenized[i].token); CheckEqual(4, tokenized[i].linenumber); } } // Known issue: //[UnitTestSharp.IgnoreTest] public void CommentEmbeddedInString() { string comment = "someString = \"An example comment: /* example */ \" "; string parsed = "someString_=_\"An example comment: /* example */ \"_"; var tokenized = UnitTestSharpHLSL.TestFinder.TokenizeHLSL(comment).ToList(); var reference = parsed.Split('_').Where(x => !string.IsNullOrEmpty(x)).ToList(); CheckEqual(tokenized.Count(), reference.Count()); for (int i = 0; i < tokenized.Count(); ++i) { CheckEqual(reference[i], tokenized[i].token); CheckEqual(1, tokenized[i].linenumber); } } public void StringEmbeddedInComment() { string text = "/*\"Some quote\"*/symbol"; var tokenized = UnitTestSharpHLSL.TestFinder.TokenizeHLSL(text).ToList(); CheckEqual(1, tokenized.Count); CheckEqual("symbol", tokenized[0].token); } } /// /// To test some regex expressions individually /// public class RegexTester : UnitTestSharp.TestFixture { public void NewLines() { string test = "a\nb\n\nc\nd\n\n\ne\n\nf\n"; var matches = System.Text.RegularExpressions.Regex.Split(test, @"(?:.*?(\n)*?)*?", System.Text.RegularExpressions.RegexOptions.Singleline | System.Text.RegularExpressions.RegexOptions.Multiline); CheckEqual(10, matches.Count(x => x == "\n")); } public void StringLiterals_1() { string text = @"He was a mess. ""Hello \""John\"","" said 'Karon'"; var matches = System.Text.RegularExpressions.Regex.Split(text, @"("".*?(? !string.IsNullOrEmpty(x)).ToList(); CheckEqual("He was a mess. ", matches[0]); CheckEqual(@"""Hello \""John\"",""", matches[1]); CheckEqual(@" said 'Karon'", matches[2]); } public void StringLiterals_2() { string text = @" char *name = ""This is a string literal with \""inner quotes\""""; "; var matches = System.Text.RegularExpressions.Regex.Split(text, @"("".*?(? !string.IsNullOrEmpty(x)).ToList(); CheckEqual(" char *name = ", matches[0]); CheckEqual(@"""This is a string literal with \""inner quotes\""""", matches[1]); CheckEqual(@"; ", matches[2]); } [UnitTestSharp.IgnoreTest] public void MultilineCComment() { string text = @"pre/**** * Common multi-line comment style. * Common multi-line comment style. * Common multi-line comment style. ****/post"; var matches = System.Text.RegularExpressions.Regex.Split(text, @"/\*(?:[^\n]*?(\n)*?)*?\*/") .Where(x => !string.IsNullOrEmpty(x)).ToList(); CheckEqual(4, matches.Count(x => x == "\n")); CheckEqual("pre", matches.First()); CheckEqual("post", matches.Last()); } [UnitTestSharp.IgnoreTest] public void Multiline() { string text = @"S Q QQ Q Q QQ QS"; var matches = System.Text.RegularExpressions.Regex.Split(text, @"S(?:[^Q]??(Q)??)*?S") .Where(x => x != " " && !string.IsNullOrEmpty(x)).ToList(); CheckEqual(8, matches.Count(x => x == "Q")); } } } public class Blockifying : UnitTestSharp.TestFixture { Func aggregator = (x, y) => new TestFinder.Token { token = x.token + " " + y.token }; public void Basic() { string text = @" void Test() { } "; var tokens = TestFinder.TokenizeHLSL(text); var blocks = TestFinder.DivideIntoBlocks(tokens); string block1 = "void Test"; string block2 = "( )"; string block3 = "{ }"; CheckEqual(block1, blocks.ElementAt(0).Aggregate(aggregator).token); CheckEqual(block2, blocks.ElementAt(1).Aggregate(aggregator).token); CheckEqual(block3, blocks.ElementAt(2).Aggregate(aggregator).token); } public void Empty() { string text = "void Test"; string block1 = "void Test"; var tokens = TestFinder.TokenizeHLSL(text); var blocks = TestFinder.DivideIntoBlocks(tokens); CheckEqual(block1, blocks.ElementAt(0).Aggregate(aggregator).token); } public void Hanging() { string text = "void Test({block}"; string block1 = "void Test"; string block2 = "( { block }"; var tokens = TestFinder.TokenizeHLSL(text); var blocks = TestFinder.DivideIntoBlocks(tokens); CheckEqual(block1, blocks.ElementAt(0).Aggregate(aggregator).token); CheckEqual(block2, blocks.ElementAt(1).Aggregate(aggregator).token); } public void TerminatingStatement() { string text = "void Test();"; string block1 = "void Test"; string block2 = "( )"; string block3 = ";"; var tokens = TestFinder.TokenizeHLSL(text); var blocks = TestFinder.DivideIntoBlocks(tokens); CheckEqual(block1, blocks.ElementAt(0).Aggregate(aggregator).token); CheckEqual(block2, blocks.ElementAt(1).Aggregate(aggregator).token); CheckEqual(block3, blocks.ElementAt(2).Aggregate(aggregator).token); } } public class FindFunctions : UnitTestSharp.TestFixture { public void Basic() { string text = @" void Test() { } "; var tests = TestFinder.FindTests(text, "sample"); CheckEqual(1, tests.Count); CheckEqual("Test", tests.First().testName); CheckEqual(2, tests.First().lineNumber); CheckEqual(text, tests.First().fileText); CheckEqual("sample", tests.First().filename); } public void IgnoresForwardDeclares() { string text = @" void Test1(); "; var tests = TestFinder.FindTests(text, "sample"); CheckEqual(0, tests.Count); } public void IgnoresNonEmptyArgumentList() { string text = @" void Test1(int a) { } "; var tests = TestFinder.FindTests(text, "sample"); CheckEqual(0, tests.Count); } public void DoesNotIgnoreEmptyArgumentList_CommentsInside() { string text = @" void Test1( /*int a */ ) { } "; var tests = TestFinder.FindTests(text, "sample"); CheckEqual(1, tests.Count); } public void EmbeddedBlocks() { string text = @" void Test1( ) { if(a == b) { DoStuff(a, b); } } void Test2( ) { if(a == b) { DoStuff(a, b); } } "; var tests = TestFinder.FindTests(text, "sample"); CheckEqual(2, tests.Count); } } } }