From a43dea916deab5f271af430ef17b767dc37e4783 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Thu, 3 Sep 2020 11:32:45 +0200 Subject: [PATCH] Fix JS tests Rewrite assertions for better error messages The difference is 32 tokens instead of 30; this is because previously, AnyTokenizer tokenized `window.alert` as a single token. Now it tokenizes it a `window`, `.`, `alert`. --- .../cpd/AnyTokenizerForTypescriptTest.java | 37 +++++++++++++++++ .../pmd/cpd/CPDCommandLineInterfaceTest.java | 11 ++--- .../pmd/cpd/testdata/ts/SampleTypeScript.ts | 9 +++++ .../pmd/cpd/testdata/ts/SampleTypeScript.txt | 40 +++++++++++++++++++ 4 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 pmd-javascript/src/test/java/net/sourceforge/pmd/cpd/AnyTokenizerForTypescriptTest.java create mode 100644 pmd-javascript/src/test/resources/net/sourceforge/pmd/cpd/testdata/ts/SampleTypeScript.ts create mode 100644 pmd-javascript/src/test/resources/net/sourceforge/pmd/cpd/testdata/ts/SampleTypeScript.txt diff --git a/pmd-javascript/src/test/java/net/sourceforge/pmd/cpd/AnyTokenizerForTypescriptTest.java b/pmd-javascript/src/test/java/net/sourceforge/pmd/cpd/AnyTokenizerForTypescriptTest.java new file mode 100644 index 0000000000..217ace1133 --- /dev/null +++ b/pmd-javascript/src/test/java/net/sourceforge/pmd/cpd/AnyTokenizerForTypescriptTest.java @@ -0,0 +1,37 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.cpd; + +import java.util.Properties; + +import org.junit.Test; + +import net.sourceforge.pmd.cpd.test.CpdTextComparisonTest; + +/** + * + */ +public class AnyTokenizerForTypescriptTest extends CpdTextComparisonTest { + + public AnyTokenizerForTypescriptTest() { + super(".ts"); + } + + @Override + protected String getResourcePrefix() { + return "testdata/ts"; + } + + @Override + public Tokenizer newTokenizer(Properties properties) { + return new AnyTokenizer(); + } + + @Test + public void testFile1() { + doTest("SampleTypeScript"); + } + +} diff --git a/pmd-javascript/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java b/pmd-javascript/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java index e17a95397a..4b92f726e5 100644 --- a/pmd-javascript/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java +++ b/pmd-javascript/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java @@ -4,7 +4,10 @@ package net.sourceforge.pmd.cpd; -import org.junit.Assert; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.emptyString; + import org.junit.Test; import net.sourceforge.pmd.cli.BaseCPDCLITest; @@ -16,8 +19,7 @@ public class CPDCommandLineInterfaceTest extends BaseCPDCLITest { "src/test/resources/net/sourceforge/pmd/cpd/ts/File1.ts", "src/test/resources/net/sourceforge/pmd/cpd/ts/File2.ts"); - String out = getOutput(); - Assert.assertTrue(out.contains("Found a 9 line (30 tokens) duplication in the following files")); + assertThat(getOutput(), containsString("Found a 9 line (32 tokens) duplication in the following files")); } @Test @@ -25,7 +27,6 @@ public class CPDCommandLineInterfaceTest extends BaseCPDCLITest { runCPD("--minimum-tokens", "5", "--language", "js", "--files", "src/test/resources/net/sourceforge/pmd/cpd/ts/"); - String out = getOutput(); - Assert.assertTrue(out.trim().isEmpty()); + assertThat(getOutput().trim(), emptyString()); } } diff --git a/pmd-javascript/src/test/resources/net/sourceforge/pmd/cpd/testdata/ts/SampleTypeScript.ts b/pmd-javascript/src/test/resources/net/sourceforge/pmd/cpd/testdata/ts/SampleTypeScript.ts new file mode 100644 index 0000000000..ba8f911c8a --- /dev/null +++ b/pmd-javascript/src/test/resources/net/sourceforge/pmd/cpd/testdata/ts/SampleTypeScript.ts @@ -0,0 +1,9 @@ +(function(){ + +var x = 1; +var y = 2; +var z = 3; +window.alert('Test'); + + +})(); \ No newline at end of file diff --git a/pmd-javascript/src/test/resources/net/sourceforge/pmd/cpd/testdata/ts/SampleTypeScript.txt b/pmd-javascript/src/test/resources/net/sourceforge/pmd/cpd/testdata/ts/SampleTypeScript.txt new file mode 100644 index 0000000000..1067e8acf6 --- /dev/null +++ b/pmd-javascript/src/test/resources/net/sourceforge/pmd/cpd/testdata/ts/SampleTypeScript.txt @@ -0,0 +1,40 @@ + [Image] or [Truncated image[ Bcol Ecol +L1 + [(] 1 1 + [function] 2 9 + [(] 10 10 + [)] 11 11 + [{] 12 12 +L3 + [var] 1 3 + [x] 5 5 + [=] 7 7 + [1] 9 9 + [;] 10 10 +L4 + [var] 1 3 + [y] 5 5 + [=] 7 7 + [2] 9 9 + [;] 10 10 +L5 + [var] 1 3 + [z] 5 5 + [=] 7 7 + [3] 9 9 + [;] 10 10 +L6 + [window] 1 6 + [.] 7 7 + [alert] 8 12 + [(] 13 13 + ['Test'] 14 19 + [)] 20 20 + [;] 21 21 +L9 + [}] 1 1 + [)] 2 2 + [(] 3 3 + [)] 4 4 + [;] 5 5 +EOF