From 7395c4d3a1ee4bc55f34dbc1dc92ebb2ba228746 Mon Sep 17 00:00:00 2001 From: paulklint Date: Wed, 10 Jul 2024 16:47:52 +0200 Subject: [PATCH] Work in progress: test suite for changes en md5 hashes - fail - still missing tests (as indicated) --- .../check/tests/ChangeAndHashTests.rsc | 172 ++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 src/org/rascalmpl/core/library/lang/rascalcore/check/tests/ChangeAndHashTests.rsc diff --git a/src/org/rascalmpl/core/library/lang/rascalcore/check/tests/ChangeAndHashTests.rsc b/src/org/rascalmpl/core/library/lang/rascalcore/check/tests/ChangeAndHashTests.rsc new file mode 100644 index 00000000..c1304052 --- /dev/null +++ b/src/org/rascalmpl/core/library/lang/rascalcore/check/tests/ChangeAndHashTests.rsc @@ -0,0 +1,172 @@ + +@synopsis{Test how md5hashes change for certain changes in a module} + +module lang::rascalcore::check::tests::ChangeAndHashTests + +import lang::rascalcore::check::Checker; +import lang::rascalcore::compile::Compile; +import util::Reflective; +import IO; +import lang::rascalcore::check::Import; +import String; +import Map; +import Set; + +TModel check(str moduleName, RascalCompilerConfig compilerConfig){ + + ModuleStatus result = rascalTModelForNames([moduleName], + compilerConfig, + dummy_compile1); + = getTModelForModule(moduleName, result); + if(found && !isEmpty(tm.messages)){ + iprintln(tm.messages); + } + return tm; +} + +bool expectEqual(str oldM, str newM, set[str] restrict = {}) { + = checkBoth(oldM, newM, restrict); + println("old: "); + println("new: "); + return old == new; +} +bool expectNotEqual(str oldM, str newM, set[str] restrict = {}){ + = checkBoth(oldM, newM, restrict); + return old != new; +} +bool expectSubset(str oldM, str newM, set[str] restrict = {}){ + = checkBoth(oldM, newM, restrict); + return old < new; +} +bool expectSuperset(str oldM, str newM, set[str] restrict = {}){ + = checkBoth(oldM, newM, restrict); + return old > new; +} + +str getRole(loc l){ + i = findFirst(l.scheme, "+"); + if(i >= 0){ + //println("getRole: =\> "); + return l.scheme[i+1..]; + } + throw "Cannot get role in "; +} + +set[loc] reduce(set[loc] locs, set[str] restrict){ + return isEmpty(restrict) ? locs : {l | l <- locs, getRole(l) in restrict }; +} + +tuple[set[loc] old, set[loc] new] checkBoth(str oldM, str newM, set[str] restrict){ + remove(|memory://stableHashProject/|, recursive=true); + + pcfg = pathConfig( + srcs=[|memory://stableHashProject/src|], + bin=|memory://stableHashProject/bin|, + generatedSources=|memory://stableHashProject/generated|, + resources=|memory://stableHashProject/resources|, + libs=[] + ); + + // First create old version of M + writeFile(|memory://stableHashProject/src/M.rsc|, "module M\n "); + + ccfg = rascalCompilerConfig(pcfg)[verbose=false][logPathConfig=false]; + + tmOld = check("M", ccfg); + if(!isEmpty(tmOld.messages)) println("old: "); + + // Remove source and generated tpls + remove(|memory://stableHashProject/|, recursive=true); + // Next create new version of M + writeFile(|memory://stableHashProject/src/M.rsc|, "module M\n"); + + tmNew = check("M", ccfg); + if(!isEmpty(tmNew.messages)) println("new: "); + return ; +} + +test bool moduleContentChanged() = + expectSubset("", "int f(int n) = n + 1;"); + +// TODO: variable declaration + +// TODO: anno declaration + +test bool funDefaultChanged() = + expectNotEqual("int f(int n) = n + 1;", "default int f(int n) = n + 1;"); + +test bool funJavaChanged() = + expectNotEqual("list[&T] delete(list[&T] _lst, int _n);", + "@javaClass{org.rascalmpl.library.Prelude} + 'java list[&T] delete(list[&T] _lst, int _n);"); + +test bool funResultChanged() = + expectNotEqual("int f(int n) = n + 1;", "value f(int n) = n + 1;"); + + test bool funParamChanged() = + expectNotEqual("num f(int n) = n + 1;", "num f(real n) = n + 1;"); + +test bool funParamLayoutChanged() = + expectEqual("int f(int n, int m) = n + m;", "int f( int n , int m ) = n + m;"); + +test bool funKwParamChanged() = + expectNotEqual("int f(int n, int delta = 1) = n + delta;", "int f(int n, int delta1) = n + delta1;"); + +test bool funBodyChanged1() = + expectEqual("int f(int n) = n + 1;", "int f(int n) = n + 2;"); + +test bool funBodyChanged2() = + expectEqual("int f(int n) { return n + 1; }", "int f(int n) { return n + 2; }"); + +test bool funWhenChanged() = + expectNotEqual("int f(int n) = n + 1 when n == 0;", "int f(int n) = n + 2 when n == 1;"); + +test bool funWithFail() = + expectNotEqual("int f(int n) { x = n + 1; fail; }", "int f(int n) { x = n + 2; fail; }"); + +test bool funsExchanged() = + expectEqual("int f(int n) = n + 1; int g(int n) = n + 1;", + "int g(int n) = n + 1; int f(int n) = n + 1;"); + +test bool funsCommentAdded() = + expectEqual("int f(int n) = n + 1; int g(int n) = n + 1;", + "int f(int n) = n + 1; /* new comment */ int g(int n) = n + 1;"); + +test bool funAdded() = + expectSubset("int f(int n) = n + 1;", + "int f(int n) = n + 1; int g(int n) = n + 2;"); + +test bool funDeleted() = + expectSuperset("int f(int n) = n + 1; int g(int n) = n + 2;", + "int f(int n) = n + 1;"); + +test bool commonKwFieldAdded() = + expectEqual("data D;", "data D(int x = 0);"); + +test bool commenKwFieldDeleted() = + expectEqual("data D(int x = 0);", "data D;"); + +test bool consAdded() = + expectSubset("data D;", "data D = d(int n);"); + +test bool consDeleted() = + expectSuperset("data D = d(int n);", "data D;"); + +test bool consFieldChanged() = + expectNotEqual("data D = d(int n);", "data D = d(int m);"); + +test bool consFieldLayoutChanged() = + expectEqual("data D = d(int n);", "data D = d( int n );"); + +// Keyword fields n and m generate separate locs, therefore we filter on constructors +test bool consKwFieldChanged() = + expectEqual("data D = d(int n = 0);", "data D = d(int m = 0);", restrict = {"constructor"}); + +test bool consKwFieldLayoutChanged() = + expectEqual("data D = d(int n = 0);", "data D = d(int n = 0);", restrict = {"constructor"}); + +// TODO: syntax declaration + +// TODO: alias declaration \ No newline at end of file