From 7b8e54df6efb3d6536feed132ca73ce87ff01519 Mon Sep 17 00:00:00 2001 From: Jake Gage Date: Tue, 20 Sep 2016 00:16:39 -0500 Subject: [PATCH 01/11] Ingores Eclipse .project configuration file --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index d650a9ea..de160cd4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.swp *.beam +.project .DS_Store tmp bin/configlet From 2bf1f43bbe866a59df45887e9e8a0d81d4f9c570 Mon Sep 17 00:00:00 2001 From: Jake Gage Date: Tue, 20 Sep 2016 00:43:38 -0500 Subject: [PATCH 02/11] Simplify setup instructions Other exercises seem to point to the Exercism.io documentation. Also, instructions for importing into IntelliJ (or other IDE) seems better off kept as advanced instructions. The tests can be run directly from the command line with edits in any editor (or IDE). These instructions may not be as complete as we'd like, but I feel we muddy the waters less by keeping the basic instructions on the simple side. --- SETUP.md | 60 ++++++-------------------------------------------------- 1 file changed, 6 insertions(+), 54 deletions(-) diff --git a/SETUP.md b/SETUP.md index 5f984e6c..045ac806 100644 --- a/SETUP.md +++ b/SETUP.md @@ -1,60 +1,12 @@ -These instructions assume you have installed exercism, Groovy, Java, and have an available IDE (this guide uses IntelliJ IDEA). If you run into trouble, consult the exercism docs on [Groovy](http://exercism.io/languages/groovy), or send us an issue on GitHub. +For installation and learning resources, refer to the +[exercism help page](http://exercism.io/languages/groovy). -## Fetch the exercise - -In a Command Prompt or Terminal, fetch the first exercise. +Run the tests by executing the test script. ``` -C:\Users\You>exercism fetch groovy - -Not Submitted: 1 problem -groovy (Hello World) Path\To\Your\Exercism\groovy\hello-world - -New: 1 problem -groovy (Hello World) Path\To\Your\Exercism\groovy\hello-world - -unchanged: 0, updated: 0, new: 1 +$ groovy ./HelloWorldSpec.groovy ``` -## Import the exercise - -1) Start IntelliJ IDEA. In the "Welcome to IntelliJ IDEA" window, click the "Open" option. - -2) Navigate to the "Path\To\Your\Exercism\groovy\THIS_EXERCISE" folder. Make sure you've selected the root directory of the exercise. Click "OK". - -3) Follow the dialog for creating the project from existing external sources. - -4) IntelliJ will then create a new project structure in the editor for your exercise. - -5) Depending on how much of the IDE you have configured before, you will likely need to add a Groovy SDK and a Java SDK (JDK 7+, ideally). These can be configured through the project/module settings by right clicking on the imported project. - -6) You may also need to configure the location of the compiler output (exercism doesn't provide all IDE files since you could work the problems in a myriad of editors instead). This can be configured in the project settings as well. - -## Start the exercise - -1) Open the `README.md` file and carefully read the background for the assignment. - -2) Start by running the test suite: In the "Project" view, right-click on the test file (`hello-world\HelloWorldTest.groovy`), select "Run", then pick the "HelloWorldTest" that has a JUnit icon to the left of it (red and green arrows). - -3) The tests are designed to fail at the beginning. Each exercise will expect you to update the associated groovy file (named the same as the test without the ````Tests```` suffix in the filename. - -4) To skip a test, you can add the @Ignore annotation a test method: - -```` - //This test will run. - @Test - void testNoName() { - assertTrue new HelloWorld().hello() == 'Hello, World!' - } - - //this test will be skipped. - @Test - @Ignore - void testSampleName() { - assertTrue new HelloWorld().hello('Alice') == 'Hello, Alice!' - } -```` - -Alternatively, you can simply comment out an entire method to have it removed from compilation and being included in test output. +After the first test(s) pass, continue by commenting out or removing the `@Ingore` annotations prepending other tests. -5) Update the main Groovy code and re-run the tests until they pass. +When all tests pass, congratulations! From 2e68500521e25842f7bc20a0e7cbcaf6829020ae Mon Sep 17 00:00:00 2001 From: Jake Gage Date: Tue, 20 Sep 2016 00:46:48 -0500 Subject: [PATCH 03/11] Implement HelloWorld test as Spock specification --- exercises/hello-world/Example.groovy | 8 +++++--- exercises/hello-world/HelloWorld.groovy | 8 +++++--- exercises/hello-world/HelloWorldSpec.groovy | 20 ++++++++++++++++++++ exercises/hello-world/HelloWorldTest.groovy | 19 ------------------- 4 files changed, 30 insertions(+), 25 deletions(-) create mode 100644 exercises/hello-world/HelloWorldSpec.groovy delete mode 100644 exercises/hello-world/HelloWorldTest.groovy diff --git a/exercises/hello-world/Example.groovy b/exercises/hello-world/Example.groovy index 9f884374..135e837f 100644 --- a/exercises/hello-world/Example.groovy +++ b/exercises/hello-world/Example.groovy @@ -1,5 +1,7 @@ class HelloWorld { - def hello(name = 'World') { - "Hello, ${name}!" - } + + def hello(name = 'World') { + "Hello, ${name}!" + } + } diff --git a/exercises/hello-world/HelloWorld.groovy b/exercises/hello-world/HelloWorld.groovy index fbfcc570..5b4b4fa5 100644 --- a/exercises/hello-world/HelloWorld.groovy +++ b/exercises/hello-world/HelloWorld.groovy @@ -1,7 +1,9 @@ // This is a skeleton file for the Groovy "Hello World" exercise. class HelloWorld { - def hello(name) { - // YOUR CODE HERE - } + + def hello(name) { + // YOUR CODE HERE + } + } diff --git a/exercises/hello-world/HelloWorldSpec.groovy b/exercises/hello-world/HelloWorldSpec.groovy new file mode 100644 index 00000000..0e2aec67 --- /dev/null +++ b/exercises/hello-world/HelloWorldSpec.groovy @@ -0,0 +1,20 @@ +@Grab('org.spockframework:spock-core:1.0-groovy-2.4') +import spock.lang.* + +class HelloWorldSpec extends Specification { + + def 'outputs "Hello, World!"'() { + expect: new HelloWorld().hello() == 'Hello, World!' + } + + @Ignore + def 'outputs "Hello, Alice!" when given the name "Alice"'() { + expect: new HelloWorld().hello('Alice') == 'Hello, Alice!' + } + + @Ignore + def 'outputs "Hello, Bob!" when given the name "Bob"'() { + expect: new HelloWorld().hello('Bob') == 'Hello, Bob!' + } + +} diff --git a/exercises/hello-world/HelloWorldTest.groovy b/exercises/hello-world/HelloWorldTest.groovy deleted file mode 100644 index 8ebadcd0..00000000 --- a/exercises/hello-world/HelloWorldTest.groovy +++ /dev/null @@ -1,19 +0,0 @@ -import org.junit.Test -import static org.junit.Assert.assertTrue - -class HelloWorldTest { - @Test - void testNoName() { - assertTrue new HelloWorld().hello() == 'Hello, World!' - } - - @Test - void testSampleName() { - assertTrue new HelloWorld().hello('Alice') == 'Hello, Alice!' - } - - @Test - void testAnotherSampleName() { - assertTrue new HelloWorld().hello('Bob') == 'Hello, Bob!' - } -} From ad238a0126bdcb7943306abfac78d8692d6193eb Mon Sep 17 00:00:00 2001 From: Jake Gage Date: Thu, 22 Sep 2016 12:18:18 -0500 Subject: [PATCH 04/11] Implement Hamming test as Spock specification --- exercises/hamming/Example.groovy | 12 +++--- exercises/hamming/HammingSpec.groovy | 50 +++++++++++++++++++++++++ exercises/hamming/HammingTest.groovy | 56 ---------------------------- 3 files changed, 57 insertions(+), 61 deletions(-) create mode 100644 exercises/hamming/HammingSpec.groovy delete mode 100644 exercises/hamming/HammingTest.groovy diff --git a/exercises/hamming/Example.groovy b/exercises/hamming/Example.groovy index 032c0e20..3786586b 100644 --- a/exercises/hamming/Example.groovy +++ b/exercises/hamming/Example.groovy @@ -1,7 +1,9 @@ class Hamming { - def compute(strand1, strand2) { - (0..[strand1.size(), strand2.size()].min() - 1).step(1).count() { - strand1[it] != strand2[it] - } - } + + def compute(strand1, strand2) { + (0..[strand1.size(), strand2.size()].min() - 1).step(1).count() { + strand1[it] != strand2[it] + } + } + } diff --git a/exercises/hamming/HammingSpec.groovy b/exercises/hamming/HammingSpec.groovy new file mode 100644 index 00000000..112a11ff --- /dev/null +++ b/exercises/hamming/HammingSpec.groovy @@ -0,0 +1,50 @@ +@Grab('org.spockframework:spock-core:1.0-groovy-2.4') +import spock.lang.* + +class HammingSpec extends Specification { + + def 'computes zero distance for identical strands'() { + expect: new Hamming().compute('A','A') == 0 + } + + @Ignore + def 'computes the distance for a single nucleotide strand'() { + expect: new Hamming().compute('A','G') == 1 + } + + @Ignore + def 'computes the distance for a small strand'() { + expect: new Hamming().compute('AG','CT') == 2 + } + + @Ignore + def 'computes a small Hamming distance'() { + expect: new Hamming().compute('AT','CT') == 1 + } + + @Ignore + def 'computes a small Hamming distance in a longer strand'() { + expect: new Hamming().compute('GGACG','GGTCG') == 1 + } + + @Ignore + def 'ignores additional nucleotides when the first strand is longer'() { + expect: new Hamming().compute('AGAGACTTA','AAA') == 1 + } + + @Ignore + def 'ignores additional nucleotides when the second strand is longer'() { + expect: new Hamming().compute('AGG','AAAACTGACCCACCCCAGG') == 2 + } + + @Ignore + def 'computes a large Hamming distance'() { + expect: new Hamming().compute('GATACA','GCATAA') == 4 + } + + @Ignore + def 'computes a very long Hamming distance'() { + expect: new Hamming().compute('GGACGGATTCTG','AGGACGGATTCT') == 9 + } + +} diff --git a/exercises/hamming/HammingTest.groovy b/exercises/hamming/HammingTest.groovy deleted file mode 100644 index 07d6a41e..00000000 --- a/exercises/hamming/HammingTest.groovy +++ /dev/null @@ -1,56 +0,0 @@ -import org.junit.Test -import static org.junit.Assert.assertEquals - -class HammingTest { - @Test - void testIdenticalStrands() { - assertEquals 0, new Hamming().compute('A','A') - } - - @Test - void testCompleteDistanceForSingleNucleotideStrand() { - assertEquals 1, new Hamming().compute('A','G') - } - - - @Test - void testCompleteDistanceForSmallStrand() { - assertEquals 2, new Hamming().compute('AG','CT') - } - - - @Test - void testSmallHammingDistance() { - assertEquals 1, new Hamming().compute('AT','CT') - } - - - @Test - void testSmallHammingDistanceInLongerStrand() { - assertEquals 1, new Hamming().compute('GGACG','GGTCG') - } - - - @Test - void testIgnoreLengthOnFirstStrandWhenLonger() { - assertEquals 1, new Hamming().compute('AGAGACTTA','AAA') - } - - - @Test - void testIgnoreLengthOnOtherStrandWhenLonger() { - assertEquals 2, new Hamming().compute('AGG','AAAACTGACCCACCCCAGG') - } - - - @Test - void testLargeHammingDistance() { - assertEquals 4, new Hamming().compute('GATACA','GCATAA') - } - - - @Test - void testVeryLongHammingDistance() { - assertEquals 9, new Hamming().compute('GGACGGATTCTG','AGGACGGATTCT') - } -} From 202fb460782764c9bed3d58d6c5254be3370b2b1 Mon Sep 17 00:00:00 2001 From: Jake Gage Date: Sun, 25 Sep 2016 23:50:54 -0500 Subject: [PATCH 05/11] Implement Gigasecond test as Spock specification --- exercises/gigasecond/Example.groovy | 16 +++++---- exercises/gigasecond/Gigasecond.groovy | 7 ++-- exercises/gigasecond/GigasecondSpec.groovy | 38 ++++++++++++++++++++++ exercises/gigasecond/GigasecondTest.groovy | 29 ----------------- 4 files changed, 52 insertions(+), 38 deletions(-) create mode 100644 exercises/gigasecond/GigasecondSpec.groovy delete mode 100644 exercises/gigasecond/GigasecondTest.groovy diff --git a/exercises/gigasecond/Example.groovy b/exercises/gigasecond/Example.groovy index 72190979..673fd60b 100644 --- a/exercises/gigasecond/Example.groovy +++ b/exercises/gigasecond/Example.groovy @@ -1,7 +1,9 @@ -class Example { - def from(Date dateObject) { - def seconds = dateObject.getTime() / 1000 - def gs = seconds + 10**9 - new Date((long) gs * 1000) - } -} \ No newline at end of file +import groovy.time.TimeCategory + +class Gigasecond { + + static Date from(Date date) { + use ( TimeCategory ) { date + (10**9).seconds } + } + +} diff --git a/exercises/gigasecond/Gigasecond.groovy b/exercises/gigasecond/Gigasecond.groovy index a4917ad3..73b3245a 100644 --- a/exercises/gigasecond/Gigasecond.groovy +++ b/exercises/gigasecond/Gigasecond.groovy @@ -1,3 +1,6 @@ class Gigasecond { - //TODO define your function here. -} \ No newline at end of file + + // YOUR CODE HERE + // HINT: methods that don't change the state of an object can be 'static' + +} diff --git a/exercises/gigasecond/GigasecondSpec.groovy b/exercises/gigasecond/GigasecondSpec.groovy new file mode 100644 index 00000000..213b2a9f --- /dev/null +++ b/exercises/gigasecond/GigasecondSpec.groovy @@ -0,0 +1,38 @@ +@Grab('org.spockframework:spock-core:1.0-groovy-2.4') +import spock.lang.* +import static java.util.Calendar.* + +class GigasecondSpec extends Specification { + + def gigasecond = new Gigasecond() + + def 'calculates one gigasecond after a date'() { + given: + def start = Date.parse('yyyy-MMM-dd', '2011-Apr-25') + when: + def result = gigasecond.from(start) + then: + result == Date.parse('yyyy-MMM-dd hh:mm:ss', '2043-Jan-01 00:46:40') + } + + @Ignore + def 'calculates one gigasecond after a date with hours and minutes'() { + given: + def start = Date.parse('yyyy-MMM-dd hh:mm', '1959-Jul-19 12:31') + when: + def result = gigasecond.from(start) + then: + result == Date.parse('yyyy-MMM-dd hh:mm:ss', '1991-Mar-27 01:17:40') + } + + @Ignore + def 'calculates one gigasecond after a date with hours and minutes and seconds'() { + given: + def start = Date.parse('yyyy-MMM-dd hh:mm:ss', '1977-Jun-13 02:15:45') + when: + def result = gigasecond.from(start) + then: + result == Date.parse('yyyy-MMM-dd hh:mm:ss', '2009-Feb-19 03:02:25') + } + +} diff --git a/exercises/gigasecond/GigasecondTest.groovy b/exercises/gigasecond/GigasecondTest.groovy deleted file mode 100644 index 280e4f30..00000000 --- a/exercises/gigasecond/GigasecondTest.groovy +++ /dev/null @@ -1,29 +0,0 @@ -import org.junit.Test -import static org.junit.Assert.assertEquals -import static java.util.Calendar.* - -/** - * Test suite for the Gigasecond exercise. - * To ignore a test case while you work the problem, simply comment out the method or add the @Ignore annotation. - */ -class GigasecondTest { - - @Test - void gigasecondFromDate() { - def gs = new Gigasecond().from(new GregorianCalendar(2011, APRIL, 25).time) - assertEquals new GregorianCalendar(2043, JANUARY, 1, 0, 46, 40).time, gs - } - - @Test - void gigasecondFromDateWithHoursAndMinutes() { - def gs = new Gigasecond().from(new GregorianCalendar(1959, JULY, 19, 12, 31).time) - assertEquals new GregorianCalendar(1991, MARCH, 27, 13, 17, 40).time, gs - } - - @Test - void gigasecondFromDateWithHoursAndMinutesAndSeconds() { - def gs = new Gigasecond().from(new GregorianCalendar(1977, JUNE, 13, 2, 15, 45).time) - assertEquals new GregorianCalendar(2009, FEBRUARY, 19, 3, 2, 25).time, gs - } - -} From f95f749a2b6b8b81aaf6f741b7ee502294e66d28 Mon Sep 17 00:00:00 2001 From: Jake Gage Date: Tue, 11 Oct 2016 15:25:14 +0200 Subject: [PATCH 06/11] Implement Raindrops test as Spock specification --- exercises/raindrops/Example.groovy | 16 ++-- exercises/raindrops/RaindropsSpec.groovy | 98 ++++++++++++++++++++++++ exercises/raindrops/RaindropsTest.groovy | 21 ----- 3 files changed, 106 insertions(+), 29 deletions(-) create mode 100644 exercises/raindrops/RaindropsSpec.groovy delete mode 100644 exercises/raindrops/RaindropsTest.groovy diff --git a/exercises/raindrops/Example.groovy b/exercises/raindrops/Example.groovy index bafb9022..5eb6a21b 100644 --- a/exercises/raindrops/Example.groovy +++ b/exercises/raindrops/Example.groovy @@ -1,13 +1,13 @@ class Raindrops { - private def primeSounds = [ 3:'Pling', 5:'Plang', 7:'Plong'] - def convert(num) { - def result = new String() + private def primeSounds = [ 3:'Pling', 5:'Plang', 7:'Plong'] - primeSounds.each({ k, v -> - if ((num % k).equals(0)) { result += v } - }) + def convert(num) { + def result = new String() + primeSounds.each({ k, v -> + if ((num % k).equals(0)) { result += v } + }) + result.isEmpty() ? num.toString() : result + } - result.isEmpty() ? num.toString() : result - } } diff --git a/exercises/raindrops/RaindropsSpec.groovy b/exercises/raindrops/RaindropsSpec.groovy new file mode 100644 index 00000000..424754e2 --- /dev/null +++ b/exercises/raindrops/RaindropsSpec.groovy @@ -0,0 +1,98 @@ +@Grab('org.spockframework:spock-core:1.0-groovy-2.4') +import spock.lang.* + +class RaindropsSpec extends Specification { + + @Shared + def raindrops = new Raindrops() + + def '1 returns 1'() { + expect: raindrops.convert(1) == '1' + } + + @Ignore + def '3, being divisible by 3, returns "Pling"'() { + expect: raindrops.convert(3) == 'Pling' + } + + @Ignore + def '5, divisible by 5, returns "Plang"'() { + expect: raindrops.convert(5) == 'Plang' + } + + @Ignore + def '7, divisible by 7, returns "Plong"'() { + expect: raindrops.convert(7) == 'Plong' + } + + @Ignore + def '6, divisible by 3, returns "Pling"'() { + expect: raindrops.convert(6) == 'Pling' + } + + @Ignore + def '8 returns the string "8"'() { + expect: raindrops.convert(8) == '8' + } + + @Ignore + def '9, divisible by 3, returns "Pling"'() { + expect: raindrops.convert(9) == 'Pling' + } + + @Ignore + def '10, divisible by 5, returns "Plang"'() { + expect: raindrops.convert(10) == 'Plang' + } + + @Ignore + def '14, divisible by 7, returns "Plong"'() { + expect: raindrops.convert(14) == 'Plong' + } + + @Ignore + def '15, divisible by both 3 and 5, returns "PlingPang"'() { + expect: raindrops.convert(15) == 'PlingPlang' + } + + @Ignore + def '21, divisible by 3 and 7, returns "PlingPlong"'() { + expect: raindrops.convert(21) == 'PlingPlong' + } + + @Ignore + def '25, divisible by 5, returns "Plang"'() { + expect: raindrops.convert(25) == 'Plang' + } + + @Ignore + def '27, divisible by 3, returns "Pling"'() { + expect: raindrops.convert(27) == 'Pling' + } + + @Ignore + def '35, divisible by 5 and 7, returns "PlangPlong"'() { + expect: raindrops.convert(35) == 'PlangPlong' + } + + @Ignore + def '49, divisible by 7, returns "Plong"'() { + expect: raindrops.convert(49) == 'Plong' + } + + @Ignore + def '52 returns the string "52"'() { + expect: raindrops.convert(52) == '52' + } + + @Ignore + def '105 returns "PlingPlangPlong"'() { + expect: raindrops.convert(105) == 'PlingPlangPlong' + } + + @Ignore + def '3125, divisible by 5, returns "Plang"'() { + expect: raindrops.convert(3125) == 'Plang' + } + +} diff --git a/exercises/raindrops/RaindropsTest.groovy b/exercises/raindrops/RaindropsTest.groovy deleted file mode 100644 index da661887..00000000 --- a/exercises/raindrops/RaindropsTest.groovy +++ /dev/null @@ -1,21 +0,0 @@ -def raindrops = new Raindrops() -try { - assert raindrops.convert(1) == '1' - assert raindrops.convert(3) == 'Pling' - assert raindrops.convert(5) == 'Plang' - assert raindrops.convert(6) == 'Pling' - assert raindrops.convert(7) == 'Plong' - assert raindrops.convert(9) == 'Pling' - assert raindrops.convert(10) == 'Plang' - assert raindrops.convert(14) == 'Plong' - assert raindrops.convert(15) == 'PlingPlang' - assert raindrops.convert(21) == 'PlingPlong' - assert raindrops.convert(25) == 'Plang' - assert raindrops.convert(35) == 'PlangPlong' - assert raindrops.convert(49) == 'Plong' - assert raindrops.convert(52) == '52' - assert raindrops.convert(105) == 'PlingPlangPlong' - assert raindrops.convert(12121) == '12121' -} catch(AssertionError error) { - println error.message -} From 4bb382bc5ea09be569a544c4c5250bee879b643f Mon Sep 17 00:00:00 2001 From: Jake Gage Date: Tue, 11 Oct 2016 16:22:59 +0200 Subject: [PATCH 07/11] Implement RNA Transcription test as Spock specification - Fix indentation - Make test cases match canonical - Implement IllegalArgumentException, per canonical data --- .../rna-transcription/ComplementSpec.groovy | 51 +++++++++++++++++ .../rna-transcription/ComplementTest.groovy | 55 ------------------- exercises/rna-transcription/Example.groovy | 33 ++++++----- 3 files changed, 70 insertions(+), 69 deletions(-) create mode 100644 exercises/rna-transcription/ComplementSpec.groovy delete mode 100644 exercises/rna-transcription/ComplementTest.groovy diff --git a/exercises/rna-transcription/ComplementSpec.groovy b/exercises/rna-transcription/ComplementSpec.groovy new file mode 100644 index 00000000..86390657 --- /dev/null +++ b/exercises/rna-transcription/ComplementSpec.groovy @@ -0,0 +1,51 @@ +@Grab('org.spockframework:spock-core:1.0-groovy-2.4') +import spock.lang.* + +class ComplementTest extends Specification { + + @Shared + def complement = new Complement() + + def 'the rna complement of cytosine is guanine'() { + expect: complement.ofDNA('C') == 'G' + } + + @Ignore + def 'the rna complement of guanine is cytosine'() { + expect: complement.ofDNA('G') == 'C' + } + + @Ignore + def 'the rna complement of thymine is adenine'() { + expect: complement.ofDNA('T') == 'A' + } + + @Ignore + def 'the rna complement of adenine is uracil'() { + expect: complement.ofDNA('A') == 'U' + } + + @Ignore + def 'can calculate long strand complement'() { + expect: complement.ofDNA('ACGTGGTCTTAA') == 'UGCACCAGAAUU' + } + + @Ignore + def 'correctly handles invalid input'() { + when: complement.ofDNA('U') + then: thrown(IllegalArgumentException) + } + + @Ignore + def 'correctly handles completely invalid input'() { + when: complement.ofDNA('XXX') + then: thrown(IllegalArgumentException) + } + + @Ignore + def 'correctly handles partially invalid input'() { + when: complement.ofDNA('ACGTXXXCTTAA') + then: thrown(IllegalArgumentException) + } + +} diff --git a/exercises/rna-transcription/ComplementTest.groovy b/exercises/rna-transcription/ComplementTest.groovy deleted file mode 100644 index 950236e7..00000000 --- a/exercises/rna-transcription/ComplementTest.groovy +++ /dev/null @@ -1,55 +0,0 @@ -import org.junit.Test -import static org.junit.Assert.assertEquals - -class ComplementTest { - @Test - void testCytosineRNAComplement() { - assertEquals 'G', new Complement().ofDNA('C') - } - - @Test - void testGuanineRNAComplement() { - assertEquals 'C', new Complement().ofDNA('G') - } - - @Test - void testThymineRNAComplement() { - assertEquals 'A', new Complement().ofDNA('T') - } - - @Test - void testAdenineRNAComplement() { - assertEquals 'U', new Complement().ofDNA('A') - } - - @Test - void testRNAComplement() { - assertEquals 'UGCACCAGAAUU', new Complement().ofDNA('ACGTGGTCTTAA') - } - - @Test - void testCytosineDNAComplement() { - assertEquals 'G', new Complement().ofRNA('C') - } - - @Test - void testGuanineDNAComplement() { - assertEquals 'C', new Complement().ofRNA('G') - } - - @Test - void testUracilDNAComplement() { - assertEquals 'A', new Complement().ofRNA('U') - } - - @Test - void testAdenineDNAComplement() { - assertEquals 'T', new Complement().ofRNA('A') - } - - @Test - void testDNAComplement() { - assertEquals 'ACTTGGGCTGTAC', new Complement().ofRNA('UGAACCCGACAUG') - } - -} diff --git a/exercises/rna-transcription/Example.groovy b/exercises/rna-transcription/Example.groovy index 2e31a905..872ba0b8 100644 --- a/exercises/rna-transcription/Example.groovy +++ b/exercises/rna-transcription/Example.groovy @@ -1,20 +1,25 @@ class Complement { - private def dnaMap = ["G":"C","C":"G","T":"A","A":"U"] - private def rnaMap = ["C":"G","G":"C","A":"T","U":"A"] - private def transcribe(seq, seqMap) { - def result = new String() - seq.split('').each() { - result += seqMap[it] + private def dnaMap = ["G":"C","C":"G","T":"A","A":"U"] + private def rnaMap = ["C":"G","G":"C","A":"T","U":"A"] + + private def transcribe(seq, seqMap) { + def result = new String() + seq.split('').each() { + if ( !seqMap[it] ) { + throw new IllegalArgumentException() + } + result += seqMap[it] + } + return result + } + + def ofDNA(strand) { + transcribe(strand, dnaMap) } - return result - } - def ofDNA(strand) { - transcribe(strand, dnaMap) - } + def ofRNA(strand) { + transcribe(strand, rnaMap) + } - def ofRNA(strand) { - transcribe(strand, rnaMap) - } } From ac2741aa413b68fabc3ee21af9e486982aba9a94 Mon Sep 17 00:00:00 2001 From: Jake Gage Date: Tue, 11 Oct 2016 17:16:18 +0200 Subject: [PATCH 08/11] Implement Squares test as Spock specification --- .../difference-of-squares/Example.groovy | 32 ++++++------ .../difference-of-squares/SquaresSpec.groovy | 30 ++++++++++++ .../difference-of-squares/SquaresTest.groovy | 49 ------------------- 3 files changed, 47 insertions(+), 64 deletions(-) create mode 100644 exercises/difference-of-squares/SquaresSpec.groovy delete mode 100644 exercises/difference-of-squares/SquaresTest.groovy diff --git a/exercises/difference-of-squares/Example.groovy b/exercises/difference-of-squares/Example.groovy index 268c1125..6e01184a 100644 --- a/exercises/difference-of-squares/Example.groovy +++ b/exercises/difference-of-squares/Example.groovy @@ -1,21 +1,23 @@ class Squares { - def naturalNum - def Squares(num) { - this.naturalNum = num - } + def naturalNum - def squareOfSums() { - (1..naturalNum).inject(0) {result, i -> result += i } ** 2 - } + def Squares(num) { + this.naturalNum = num + } - def sumOfSquares() { - (1..naturalNum).inject(0) {result, i -> - result += i ** 2 - } - } + def squareOfSums() { + (1..naturalNum).inject(0) {result, i -> result += i } ** 2 + } + + def sumOfSquares() { + (1..naturalNum).inject(0) {result, i -> + result += i ** 2 + } + } + + def difference() { + squareOfSums() - sumOfSquares() + } - def difference() { - squareOfSums() - sumOfSquares() - } } diff --git a/exercises/difference-of-squares/SquaresSpec.groovy b/exercises/difference-of-squares/SquaresSpec.groovy new file mode 100644 index 00000000..d14b4a0d --- /dev/null +++ b/exercises/difference-of-squares/SquaresSpec.groovy @@ -0,0 +1,30 @@ +@Grab('org.spockframework:spock-core:1.0-groovy-2.4') +import spock.lang.* + +class SquaresSpec extends Specification { + + def 'can square the sum of the numbers up to the given number'() { + expect: + new Squares(5).squareOfSums() == 225 + new Squares(10).squareOfSums() == 3025 + new Squares(100).squareOfSums() == 25502500 + } + + @Ignore + def 'can sum the squares of the numbers up to the given number'() { + expect: + new Squares(5).sumOfSquares() == 55 + new Squares(10).sumOfSquares() == 385 + new Squares(100).sumOfSquares() == 338350 + } + + @Ignore + def 'can subtract sum of squares from square of sums'() { + expect: + new Squares(0).difference() == 0 + new Squares(5).difference() == 170 + new Squares(10).difference() == 2640 + new Squares(100).difference() == 25164150 + } + +} diff --git a/exercises/difference-of-squares/SquaresTest.groovy b/exercises/difference-of-squares/SquaresTest.groovy deleted file mode 100644 index 1f20e236..00000000 --- a/exercises/difference-of-squares/SquaresTest.groovy +++ /dev/null @@ -1,49 +0,0 @@ -import org.junit.Test -import static org.junit.Assert.assertEquals - -class SquaresTest { - @Test - void squareOfSumsTo5() { - assertEquals 225, new Squares(5).squareOfSums() - } - - @Test - void sumOfSquaresTo5() { - assertEquals 55, new Squares(5).sumOfSquares() - } - - @Test - void diffOfSumsTo5() { - assertEquals 170, new Squares(5).difference() - } - - @Test - void squareOfSumsTo10() { - assertEquals 3025, new Squares(10).squareOfSums() - } - - @Test - void sumOfSquaresTo10() { - assertEquals 385, new Squares(10).sumOfSquares() - } - - @Test - void diffOfSumsTo10() { - assertEquals 2640, new Squares(10).difference() - } - - @Test - void squareOfSumsTo100() { - assertEquals 25502500, new Squares(100).squareOfSums() - } - - @Test - void sumOfSquaresTo100() { - assertEquals 338350, new Squares(100).sumOfSquares() - } - - @Test - void diffOfSumsTo100() { - assertEquals 25164150, new Squares(100).difference() - } -} From 9da46df727732300afc8c86de5950dfe73e0c5a8 Mon Sep 17 00:00:00 2001 From: Jake Gage Date: Mon, 31 Oct 2016 19:32:21 -0500 Subject: [PATCH 09/11] Corrcet tpyo --- SETUP.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SETUP.md b/SETUP.md index 045ac806..431080fd 100644 --- a/SETUP.md +++ b/SETUP.md @@ -7,6 +7,6 @@ Run the tests by executing the test script. $ groovy ./HelloWorldSpec.groovy ``` -After the first test(s) pass, continue by commenting out or removing the `@Ingore` annotations prepending other tests. +After the first test(s) pass, continue by commenting out or removing the `@Ignore` annotations prepending other tests. When all tests pass, congratulations! From 1cb065947d2f48294f544b40c34f504494807666 Mon Sep 17 00:00:00 2001 From: Jake Gage Date: Mon, 31 Oct 2016 19:50:54 -0500 Subject: [PATCH 10/11] Use parameterized inputs in Squares specification --- .../difference-of-squares/SquaresSpec.groovy | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/exercises/difference-of-squares/SquaresSpec.groovy b/exercises/difference-of-squares/SquaresSpec.groovy index d14b4a0d..0e92c1cc 100644 --- a/exercises/difference-of-squares/SquaresSpec.groovy +++ b/exercises/difference-of-squares/SquaresSpec.groovy @@ -3,28 +3,40 @@ import spock.lang.* class SquaresSpec extends Specification { - def 'can square the sum of the numbers up to the given number'() { + @Unroll("can square the sum of the numbers up to #integer") + def "can square the sum up to the given integer"() { expect: - new Squares(5).squareOfSums() == 225 - new Squares(10).squareOfSums() == 3025 - new Squares(100).squareOfSums() == 25502500 + new Squares(integer).squareOfSums() == result + where: + integer | result + 5 | 225 + 10 | 3025 + 100 | 25502500 } @Ignore - def 'can sum the squares of the numbers up to the given number'() { + @Unroll("can sum the squares up to #integer") + def 'can sum the squares up to the given integer'() { expect: - new Squares(5).sumOfSquares() == 55 - new Squares(10).sumOfSquares() == 385 - new Squares(100).sumOfSquares() == 338350 + new Squares(integer).sumOfSquares() == result + where: + integer | result + 5 | 55 + 10 | 385 + 100 | 338350 } @Ignore + @Unroll("can subtract sum of squares from square of sums of #integer") def 'can subtract sum of squares from square of sums'() { expect: - new Squares(0).difference() == 0 - new Squares(5).difference() == 170 - new Squares(10).difference() == 2640 - new Squares(100).difference() == 25164150 + new Squares(integer).difference() == result + where: + integer | result + 0 | 0 + 5 | 170 + 10 | 2640 + 100 | 25164150 } } From 2710bf2d4726c602587ad14a502c33b67549eb5f Mon Sep 17 00:00:00 2001 From: Jake Gage Date: Thu, 3 Nov 2016 13:40:38 -0500 Subject: [PATCH 11/11] Extracts class instantiation in test --- exercises/hello-world/HelloWorldSpec.groovy | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/exercises/hello-world/HelloWorldSpec.groovy b/exercises/hello-world/HelloWorldSpec.groovy index 0e2aec67..4fc91e46 100644 --- a/exercises/hello-world/HelloWorldSpec.groovy +++ b/exercises/hello-world/HelloWorldSpec.groovy @@ -3,18 +3,21 @@ import spock.lang.* class HelloWorldSpec extends Specification { + @Shared + def hello = new HelloWorld() + def 'outputs "Hello, World!"'() { - expect: new HelloWorld().hello() == 'Hello, World!' + expect: hello.hello() == 'Hello, World!' } @Ignore def 'outputs "Hello, Alice!" when given the name "Alice"'() { - expect: new HelloWorld().hello('Alice') == 'Hello, Alice!' + expect: hello.hello('Alice') == 'Hello, Alice!' } @Ignore def 'outputs "Hello, Bob!" when given the name "Bob"'() { - expect: new HelloWorld().hello('Bob') == 'Hello, Bob!' + expect: hello.hello('Bob') == 'Hello, Bob!' } }