-
-
Notifications
You must be signed in to change notification settings - Fork 658
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nucleotide-count: add generator and update example (#1014)
* implemented generator and new example for pig latin * go fmt * small refactoring on @ferhatelmas suggestions * each test gets a description printed and PASS or FAIL * implemented test generator for nucleotide-count exercise (issue #605) * added hints.md and changed .expected type in generator * restored original nucleotide_count.go stub and adjusted expected type for tests * small refactoring in example.go * problem specification changed format; check for unexpected type in generator
- Loading branch information
1 parent
35f0d77
commit c1d260a
Showing
5 changed files
with
168 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
"text/template" | ||
|
||
"../../../gen" | ||
) | ||
|
||
func main() { | ||
t, err := template.New("").Parse(tmpl) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
var j js | ||
if err := gen.Gen("nucleotide-count", &j, t); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
// The JSON structure we expect to be able to unmarshal into | ||
type js struct { | ||
exercise string | ||
version string | ||
Cases []struct { | ||
Description string | ||
Cases []OneCase | ||
} | ||
} | ||
|
||
// OneCase represents each test case | ||
type OneCase struct { | ||
Description string | ||
Property string | ||
Input struct { | ||
Strand string | ||
} | ||
Expected map[string]interface{} | ||
} | ||
|
||
// ErrorExpected returns true if an error should be raised | ||
func (c OneCase) ErrorExpected() bool { | ||
_, exists := c.Expected["error"] | ||
return exists | ||
} | ||
|
||
// SortedMapString collects key:values for a map in sorted order | ||
func (c OneCase) SortedMapString() string { | ||
strs := make([]string, 0, len(c.Expected)) | ||
for k, v := range c.Expected { | ||
switch t := v.(type) { | ||
case float64: | ||
strs = append(strs, `'`+k+`': `+strconv.FormatFloat(t, 'f', -1, 64)) | ||
default: | ||
log.Fatalf("unexpected type %T for %v", t, v) | ||
} | ||
|
||
} | ||
sort.Strings(strs) | ||
return strings.Join(strs, ",") | ||
} | ||
|
||
// template applied to above data structure generates the Go test cases | ||
var tmpl = `package dna | ||
{{.Header}} | ||
{{range .J.Cases}}// {{.Description}} | ||
var testCases = []struct { | ||
description string | ||
strand string | ||
expected Histogram | ||
errorExpected bool | ||
}{ | ||
{{range .Cases}}{ | ||
description: {{printf "%q" .Description}}, | ||
strand: {{printf "%#v" .Input.Strand}}, | ||
{{if .ErrorExpected}}errorExpected: true, | ||
{{else}}expected: Histogram{ {{.SortedMapString}} }, | ||
{{- end}} | ||
}, | ||
{{end}}{{end}} | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
## Implementation | ||
|
||
You should define a custom type 'DNA' with a function 'Counts' that outputs two values: | ||
|
||
- a frequency count for the given DNA strand | ||
- an error (if there are invalid nucleotides) | ||
|
||
Which is a good type for a DNA strand ? | ||
|
||
Which is the best Go types to represent the output values ? | ||
|
||
Take a look at the test cases to get a hint about what could be the possible inputs. | ||
|
||
|
||
## note about the tests | ||
You may be wondering about the `cases_test.go` file. We explain it in the | ||
[leap exercise][leap-exercise-readme]. | ||
|
||
[leap-exercise-readme]: https://github.com/exercism/go/blob/master/exercises/leap/README.md | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package dna | ||
|
||
// Source: exercism/problem-specifications | ||
// Commit: 879a096 nucleotide-count: Apply new "input" policy | ||
// Problem Specifications Version: 1.3.0 | ||
|
||
// count all nucleotides in a strand | ||
var testCases = []struct { | ||
description string | ||
strand string | ||
expected Histogram | ||
errorExpected bool | ||
}{ | ||
{ | ||
description: "empty strand", | ||
strand: "", | ||
expected: Histogram{'A': 0, 'C': 0, 'G': 0, 'T': 0}, | ||
}, | ||
{ | ||
description: "can count one nucleotide in single-character input", | ||
strand: "G", | ||
expected: Histogram{'A': 0, 'C': 0, 'G': 1, 'T': 0}, | ||
}, | ||
{ | ||
description: "strand with repeated nucleotide", | ||
strand: "GGGGGGG", | ||
expected: Histogram{'A': 0, 'C': 0, 'G': 7, 'T': 0}, | ||
}, | ||
{ | ||
description: "strand with multiple nucleotides", | ||
strand: "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC", | ||
expected: Histogram{'A': 20, 'C': 12, 'G': 17, 'T': 21}, | ||
}, | ||
{ | ||
description: "strand with invalid nucleotides", | ||
strand: "AGXXACT", | ||
errorExpected: true, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters