Skip to content

Commit

Permalink
added TestDictionary, TestIteration, TestBoolean, TestException, Test…
Browse files Browse the repository at this point in the history
…Class, refactored existing koans
  • Loading branch information
skim committed Jun 20, 2011
1 parent 0e0e8fa commit 62203f9
Show file tree
Hide file tree
Showing 19 changed files with 418 additions and 61 deletions.
21 changes: 13 additions & 8 deletions README.markdown
Expand Up @@ -63,17 +63,20 @@ Ok, have at it!
* TestSortedCollection
* TestBag
* TestAssociation
\* TestDictionary
* TestDictionary
* TestSet
* TestBlock
* TestCollectionsRevisited
\* TestIteration
\* TestBoolean
\* TestException
* TestIteration
* TestBoolean
* TestException
* TestRegex
\* TestClass
\* TestClassHierarchy
\* TestMetaclass
* TestClass

### Koans that still need to be written

* TestClassHierarchy
* TestMetaclass

### Pull Requests and Feedback are Welcome!

Expand All @@ -91,7 +94,9 @@ There is no set time, but I plan to port these koans over to the following Small
* redline?
* Jtalk?

### Credits
### Inspiration and Credits

GNU Smalltalk Koans were inspired by Edgecase's Ruby Koans and Aaron Bedra's Clojure Koans (now maintained by Colin Jones).

Thanks to [Matt Yoho](http://twitter.com/#!/mattyoho) of the Ruby Koans team for letting me port some of the koans over.

Expand Down
4 changes: 2 additions & 2 deletions src/koans/TestArray.st
Expand Up @@ -47,7 +47,7 @@ Koan subclass: TestArray [
array := Array with: 2 with: 4 with: 6 with: 8 with: 10.

self expect: (self fillMeIn) toEqual: (array).
self expect: [Array with: 1 with: 3 with: 5 with: 7 with: 9 with: 11] toRaise: (self fillMeIn).
self expect: [ Array with: 1 with: 3 with: 5 with: 7 with: 9 with: 11 ] toRaise: (self fillMeIn).
]

testReplaceValueAtElement [
Expand All @@ -64,7 +64,7 @@ Koan subclass: TestArray [

array := #('a' 'b' 'c').

self expect: [array at: 1 put: 'd'] toRaise: (self fillMeIn).
self expect: [ array at: 1 put: 'd' ] toRaise: (self fillMeIn).

"#() only accepts literal values."
]
Expand Down
2 changes: 2 additions & 0 deletions src/koans/TestAssociation.st
@@ -1,4 +1,6 @@
Koan subclass: TestAssociation [
<comment: 'A collection of association tests.'>

testCreatingAssociationThreeDifferentWays [
| association anotherAssociation andAnotherAssociation |

Expand Down
2 changes: 1 addition & 1 deletion src/koans/TestBag.st
Expand Up @@ -21,7 +21,7 @@ Koan subclass: TestBag [
bag add: 1 withOccurrences: 3.
bag add: 2.

self expect: [bag at: 1] toRaise: (self fillMeIn).
self expect: [ bag at: 1 ] toRaise: (self fillMeIn).
]

testComparingBags [
Expand Down
4 changes: 2 additions & 2 deletions src/koans/TestBlock.st
Expand Up @@ -66,7 +66,7 @@ Koan subclass: TestBlock [

block := [ :x | x ].

self expect: [block value: 1 value: 2] toRaise: (self fillMeIn).
self expect: [ block value: 1 value: 2 ] toRaise: (self fillMeIn).
]

testIterateUsingInterval [
Expand Down Expand Up @@ -116,7 +116,7 @@ Koan subclass: TestBlock [
block := [].

self expect: (self fillMeIn) toEqual: (block cull: 1).
self expect: [block value: 1] toRaise: (self fillMeIn).
self expect: [ block value: 1 ] toRaise: (self fillMeIn).
]

testEachBlockIsAUniqueObject [
Expand Down
79 changes: 67 additions & 12 deletions src/koans/TestBoolean.st
@@ -1,17 +1,72 @@
Koan subclass: TestBoolean [
<comment: 'A collection of boolean tests.'>

testIfTrueAndIfFalse [
| result anotherResult |

(1 < 2)
ifTrue: [ result := true ]
ifFalse: [ result := false ].
((5 + 2 * 9) < 25 )
ifTrue: [ anotherResult := true ]
ifFalse: [ anotherResult := false ].

self expect: (self fillMeIn) toEqual: (result).
self expect: (self fillMeIn) toEqual: (anotherResult).
truthValue: condition [
condition ifTrue: [ ^#trueStuff ].
condition ifFalse: [ ^#falseStuff ].
]

testTrueIsTreatedAsTrue [
self expect: (self fillMeIn) toEqual: (self truthValue: true).
]

testFalseIsTreatedAsFalse [
self expect: (self fillMeIn) toEqual: (self truthValue: false).
]

testNilIsNotTreatedAsABoolean [
self expect: [ self truthValue: nil ] toRaise: (self fillMeIn).
]

testIfTrueAndIfFalseOnlyRespondsToBooleans [
self expect: [ self truthValue: 1 ] toRaise: (self fillMeIn).
self expect: [ self truthValue: 0 ] toRaise: (self fillMeIn).
self expect: [ self truthValue: #() ] toRaise: (self fillMeIn).
self expect: [ self truthValue: 'String' ] toRaise: (self fillMeIn).
self expect: [ self truthValue: '' ] toRaise: (self fillMeIn).
]

testAndShortCircuit [
| x y z |

x := 4. y := 10. z := 0.

self expect: (self fillMeIn) toEqual: (x < 5 and: [ z := 1. y < 11 ]).
self expect: (self fillMeIn) toEqual: z.

z := 0.

self expect: (self fillMeIn) toEqual: (x < 4 and: [ z := 1. y < 11 ]).
self expect: (self fillMeIn) toEqual: z.

"How is this different from '&'?"
]

testOrShortCircuit [
| x y z |

x := 4. y := 10. z := 0.

self expect: (self fillMeIn) toEqual: (x > 5 or: [ z := 1. y > 9]).
self expect: (self fillMeIn) toEqual: z.

z := 0.

self expect: (self fillMeIn) toEqual: (x > 9 sqrt or: [ z := 1. y > 9]).
self expect: (self fillMeIn) toEqual: z.
]

testEqv [
self expect: (self fillMeIn) toEqual: (true eqv: true).
self expect: (self fillMeIn) toEqual: (true eqv: false).
self expect: (self fillMeIn) toEqual: (false eqv: true).
self expect: (self fillMeIn) toEqual: (false eqv: false).
]

testXor [
self expect: (self fillMeIn) toEqual: (true xor: true).
self expect: (self fillMeIn) toEqual: (true xor: false).
self expect: (self fillMeIn) toEqual: (false xor: true).
self expect: (self fillMeIn) toEqual: (false xor: false).
]
]
85 changes: 81 additions & 4 deletions src/koans/TestClass.st
@@ -1,13 +1,90 @@
Koan subclass: TestClass [
<comment: 'A collection of class tests.'>
Object subclass: HelloWorld [
<comment: 'This is a HelloWorld comment'>
<category: 'This is a HelloWorld category'>

Statement := $S.

| instanceVariable |

"Unlike other Smalltalks, method statements are inside brackets."

"Class variables are defined the same as variable assignments."
awesome [
^true
]

+ string [
^'Hello ', string
]

"Pragmas define class comment, class category, imported namespaces, and the shape of indexed instance variables."
say: something [
^self class name asString, ' says ', something
]
]

HelloWorld subclass: SubHelloWorld []

Koan subclass: TestClass [
<comment: 'A collection of class tests.'>

"Class variables are defined the same as variable assignments."

"superclass"
"allInstances"
"allSuperclasses"

testName [
self expect: (self fillMeIn) toEqual: HelloWorld name.
]

testPragmas [
"
Pragmas define:
class comment
class category
imported namespaces
shape of indexed instance variables.
"

self expect: (self fillMeIn) toEqual: HelloWorld comment.
self expect: (self fillMeIn) toEqual: HelloWorld category.
]

testInstanceVariableCollection [
self expect: (self fillMeIn) toEqual: HelloWorld instVarNames.
]

testClassInstanceVariableCollection [
self expect: (self fillMeIn) toEqual: HelloWorld classVarNames asArray.
]

testMessageCollection [
self expect: (self fillMeIn) toEqual: HelloWorld selectors asArray.
]

testSourceCodeAt [
"
#sourceCodeAt: returns the source code of the specified message.
For example: HelloWorld sourceCodeAt: #+ would return:
'+ string [
^''Hello '', string
]'
"
]

testAllInstances [
| helloWorld |

helloWorld := HelloWorld new.

self expect: (self fillMeIn) toEqual: (HelloWorld allInstances asArray).
]

testSuperclass [
self expect: (self fillMeIn) toEqual: (HelloWorld superclass).
]

testAllSubclasses [
self expect: (self fillMeIn) toEqual: (HelloWorld allSubclasses asArray).
]
]
7 changes: 7 additions & 0 deletions src/koans/TestClassHierarchy.st
@@ -1,6 +1,13 @@
Koan subclass: TestClassHierarchy [
<comment: 'A collection of class hierarchy tests.'>

testSuperclasses [
self expect: (self fillMeIn) toEqual: (SmallInteger superclass).
self expect: (self fillMeIn) toEqual: (String superclass).
self expect: (self fillMeIn) toEqual: (String superclass).
self expect: (self fillMeIn) toEqual: (True superclass).
]

testNumberAndMagnitudeAreAbstractClasses [
self expect: (self fillMeIn) toEqual: (1234 isKindOf: Number).
self expect: (self fillMeIn) toEqual: (1234 isKindOf: Magnitude).
Expand Down
4 changes: 2 additions & 2 deletions src/koans/TestCollectionsRevisited.st
Expand Up @@ -157,7 +157,7 @@ Koan subclass: TestCollectionsRevisited [
array := #(0 1 2 3).

self expect: (self fillMeIn) toEqual: (array at: 0 ifAbsent: [nil]).
self expect: [array at: 0] toRaise: (self fillMeIn).
self expect: [ array at: 0 ] toRaise: (self fillMeIn).

"#at:ifAbsent: also works on OrderedCollection, SortedCollection, and Dictionary."
]
Expand All @@ -168,6 +168,6 @@ Koan subclass: TestCollectionsRevisited [
orderedCollection := OrderedCollection new addAll: #(0 1 2 3); yourself.

self expect: (self fillMeIn) toEqual: (orderedCollection remove: 4 ifAbsent: [nil]).
self expect: [orderedCollection remove: 4] toRaise: (self fillMeIn).
self expect: [ orderedCollection remove: 4 ] toRaise: (self fillMeIn).
]
]

0 comments on commit 62203f9

Please sign in to comment.