1. Kink Programming Language
  2. Untitled project
  3. kink

Commits

Taku Miyakawa  committed 15fdc49

Refines the test script of SET module

  • Participants
  • Parent commits 5d09332
  • Branches default

Comments (0)

Files changed (1)

File src/test/kink/SET_test.kn

View file
 #!/usr/bin/env kink
-# vim: et sw=4 sts=4
+# vim: et sw=4 sts=4 fdm=marker
 
 # Copyright (c) 2013 Miyakawa Taku
 # 
 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 # THE SOFTWARE.
 
-use('TEST.test')
 use('SET.set')
+
+use('COMPARE')
 use('COMMON_PATTERN.eq')
 use('COMMON_PATTERN.ne')
 use('COMMON_PATTERN.or')
 use('COMMON_PATTERN.not')
 use('COMMON_PATTERN.identical_to')
+use('TEST.test_group')
+use('TEST.test')
+use('TEST.set_up')
 
-# Tests Set.count, Set.any? and Set.empty?
-test {
-    :Set = set('foo' 'bar' 'baz')
-    Set.count.check(3)
-    Set.any?.check(true)
-    Set.empty?.check(false)
-}
+test_group('An empty set') { # {{{1
 
-# Tests Set.count, Set.any? and Set.empty?
-test {
-    :Set = set
-    Set.count.check(0)
-    Set.any?.check(false)
-    Set.empty?.check(true)
-}
+    set_up(:Empty_set) {
+        set
+    }
 
-# Tests Set.has?
-test {
-    :Set = set('foo' 'bar')
-    Set.has?('foo').check(true)
-    Set.has?('xxx').check(false)
-}
+    test('.any? says it has no element') {
+        Empty_set.any?.check(false)
+    }
 
-# Tests Set.push
-test {
-    :Set = set('twift' 'voxxx')
-    Set.push('peng').push('olv')
-    Set.has?('twift').check(true)
-    Set.has?('peng').check(true)
-    Set.has?('olv').check(true)
-    Set.has?('xxx').check(false)
-}
+    test('.empty? says it has no element') {
+        Empty_set.empty?.check(true)
+    }
 
-# Tests Set.delete
-test {
-    :Set = set('twift' 'voxxx' 'peng')
-    :Result = Set.delete('peng')
-    Result.check(true)
-    Set.has?('twift').check(true)
-    Set.has?('voxxx').check(true)
-    Set.has?('peng').check(false)
-}
+    test('.count says it has no element') {
+        Empty_set.count.check(0)
+    }
 
-# Tests Set.append
-test {
-    :Set = set('twift' 'voxxx')
-    Set.append(set('peng' 'olv')).append(set('rustc'))
-    Set.has?('twift').check(true)
-    Set.has?('voxxx').check(true)
-    Set.has?('peng').check(true)
-    Set.has?('olv').check(true)
-    Set.has?('rustc').check(true)
-    Set.has?('xxx').check(false)
-}
+} # }}}1
 
-# Tests equality
-test {
-    use('CHECK.check_equality')
-    check_equality(
-        [set('twift' 'voxxx') set('twift' 'voxxx')]
-        [set('twift' 'voxxx' 'olv') set('twift' 'voxxx' 'olv')]
-        [set('olv' 'peng') set('olv' 'peng')]
-        [set().parent set()]
-        [module('JAVA').null]
-        [42]
-    )
-}
+test_group('set(Hoge Fuga)') { # {{{1
 
-# Tests Set.clone
-test {
-    :Set = set('twift' 'voxxx')
-    :Clone = Set.clone
-    Clone.check(eq(Set))
-    Clone.check(not(identical_to(Set)))
-}
+    set_up(:Hoge) { value('show' { 'Hoge' }) }
+    set_up(:Fuga) { value('show' { 'Fuga' }) }
+    set_up(:Piyo) { value('show' { 'Piyo' }) }
 
-# Tests Set + ANOTHER_SET
-test {
-    (set('twift' 'voxxx') + set('twift' 'peng')).check(eq(set('twift' 'voxxx' 'peng')))
-}
+    set_up(:Hoge_fuga_set) {
+        set(Hoge Fuga)
+    }
 
-# Tests Set - ANOTHER_SET
-test {
-    (set('twift' 'voxxx') - set('twift' 'peng')).check(eq(set('voxxx')))
-}
+    test('.any? says it has some elements') {
+        Hoge_fuga_set.any?.check(true)
+    }
 
-# Tests Set & ANOTHER_SET
-test {
-    (set('twift' 'voxxx') & set('twift' 'peng')).check(eq(set('twift')))
-}
+    test('.empty? says it has some elements') {
+        Hoge_fuga_set.empty?.check(false)
+    }
 
-# Tests Set.map
-test {
-    set('twift' 'voxxx').map { (:E) '$' + E } .check(set('$twift' '$voxxx'))
-}
+    test('.count says it has 2 elements') {
+        Hoge_fuga_set.count.check(2)
+    }
 
-# Tests Set.concat_map
-test {
-    set('twift' 'voxxx').concat_map { (:E) [('$' + E) E] } .check(set('twift' 'voxxx' '$twift' '$voxxx'))
-}
+    test_group('.has?') {
+        test('says it has Hoge as an element') {
+            Hoge_fuga_set.has?(Hoge).check(true)
+        }
 
-# Tests Set.select
-test {
-    set('twift' 'VOXXX' 'peng' 'OLV').select(%'[a-z]+').check(set('twift' 'peng'))
-}
+        test('says it does not have Piyo as an element') {
+            Hoge_fuga_set.has?(Piyo).check(false)
+        }
+    }
 
-# Tests Set.all?
-test {
-    set('TWIFT' 'VOXXX' 'PENG' 'OLV').all?(%'[a-z]+').check(false)
-    set('twift' 'VOXXX' 'peng' 'OLV').all?(%'[a-z]+').check(false)
-    set('twift' 'voxxx' 'peng' 'olv').all?(%'[a-z]+').check(true)
-}
+    test_group('.push') {
+        test('adds a new element') {
+            :Result = Hoge_fuga_set.push(Piyo)
+            Result.check(identical_to(Hoge_fuga_set))
+            Hoge_fuga_set.has?(Piyo).check(true)
+        }
 
-# Tests Set.any?
-test {
-    set('TWIFT' 'VOXXX' 'PENG' 'OLV').any?(%'[a-z]+').check(false)
-    set('twift' 'VOXXX' 'peng' 'OLV').any?(%'[a-z]+').check(true)
-    set('twift' 'voxxx' 'peng' 'olv').any?(%'[a-z]+').check(true)
-}
+        test('Does nothing for an existing element') {
+            :Result = Hoge_fuga_set.push(Hoge)
+            Result.check(identical_to(Hoge_fuga_set))
+            Hoge_fuga_set.count.check(2)
+            Hoge_fuga_set.has?(Hoge).check(true)
+        }
+    }
 
-# Tests Set.subset_of?
-test {
-    set('twift' 'voxxx').subset_of?(set('twift' 'peng')).check(false)
-    set('twift' 'voxxx').subset_of?(set('twift' 'voxxx')).check(true)
-    set('twift' 'voxxx').subset_of?(set('twift' 'voxxx' 'peng')).check(true)
-}
+    test_group('.delete') {
+        test('deletes the specified element') {
+            :Deleted? = Hoge_fuga_set.delete(Hoge)
+            Deleted?.check(true)
+            Hoge_fuga_set.has?(Hoge).check(false)
+            Hoge_fuga_set.has?(Fuga).check(true)
+        }
 
-# Tests Set.loop
-test {
-    :List = []
-    set('twift' 'voxxx' 'peng').loop { (:Element)
-        List.push(Element)
+        test('does nothing for a key which does not exist') {
+            :Deleted? = Hoge_fuga_set.delete(Piyo)
+            Deleted?.check(false)
+            Hoge_fuga_set.has?(Hoge).check(true)
+            Hoge_fuga_set.has?(Fuga).check(true)
+            Hoge_fuga_set.has?(Piyo).check(false)
+        }
     }
-    List.sort.check(['peng' 'twift' 'voxxx'])
-}
 
-# Tests Set.seq
-test {
-    set('twift' 'voxxx' 'peng' 'olv').seq.list.sort.check(['olv' 'peng' 'twift' 'voxxx'])
-}
+    test('.clone makes a new set equal to the set itself') {
+        :Clone_set = Hoge_fuga_set.clone
+        Clone_set.check(not(identical_to(Hoge_fuga_set)))
+        Clone_set.count.check(2)
+        Clone_set.has?(Hoge).check(true)
+        Clone_set.has?(Fuga).check(true)
+    }
 
-# Tests Set.list
-test {
-    set('twift' 'voxxx' 'peng' 'olv').list.sort.check(['olv' 'peng' 'twift' 'voxxx'])
-}
+    test('.map transforms each element to an element') {
+        :Show_set = Hoge_fuga_set.map { (:Element)
+            Element.show
+        }
+        Show_set.count.check(2)
+        Show_set.has?('Hoge').check(true)
+        Show_set.has?('Fuga').check(true)
+    }
 
-# Tests Set.show
-test {
-    set('twift' 'voxxx').show.check(or("set('twift' 'voxxx')" "set('voxxx' 'twift')"))
-}
+    test('.concat_map transforms each element to multiple elements') {
+        :Mapped_set = Hoge_fuga_set.concat_map { (:Element)
+            [Element.show (Element.show + Element.show)]
+        }
+        Mapped_set.count.check(4)
+        Mapped_set.has?('Hoge').check(true)
+        Mapped_set.has?('HogeHoge').check(true)
+        Mapped_set.has?('Fuga').check(true)
+        Mapped_set.has?('FugaFuga').check(true)
+    }
 
-# Tests the prototype of maps
-test {
-    :Proto = set().parent
-    Proto.check(eq(set()))
-}
+    test('.loop loops for elements') {
+        :Accumulator = []
+        Hoge_fuga_set.loop { (:Element)
+            Accumulator.push(Element)
+        }
+        Accumulator.sort(COMPARE.by { \0.show }).check([Fuga Hoge])
+    }
 
-# Tests Set.set??
-test {
-    set().set??.check(true)
-}
+    test('.seq makes a sequence of the elements') {
+        :Seq = Hoge_fuga_set.seq
+        Seq.list.sort(COMPARE.by { \0.show }).check([Fuga Hoge])
+    }
+
+    test('.list makes a list of the elements') {
+        :List = Hoge_fuga_set.list
+        List.sort(COMPARE.by { \0.show }).check([Fuga Hoge])
+    }
+
+    test('.show returns a string representation') {
+        Hoge_fuga_set.show.check(or(
+            'set(Hoge Fuga)'
+            'set(Fuga Hoge)'
+        ))
+    }
+
+    test('.set?? returns true to indicate that it is a set') {
+        Hoge_fuga_set.set??.check(true)
+    }
+
+    test_group('with set(Fuga Piyo)') {
+        set_up(:Fuga_piyo_set) {
+            set(Fuga Piyo)
+        }
+
+        test('.append merges the argument to the receiver') {
+            :Result = Hoge_fuga_set.append(Fuga_piyo_set)
+            Result.check(identical_to(Hoge_fuga_set))
+            Hoge_fuga_set.count.check(3)
+            Hoge_fuga_set.has?(Hoge).check(true)
+            Hoge_fuga_set.has?(Fuga).check(true)
+            Hoge_fuga_set.has?(Piyo).check(true)
+        }
+
+        test('.op_add produces a sum of the sets') {
+            :Sum = Hoge_fuga_set + Fuga_piyo_set
+            Sum.count.check(3)
+            Sum.has?(Hoge).check(true)
+            Sum.has?(Fuga).check(true)
+            Sum.has?(Piyo).check(true)
+        }
+
+        test('.op_sub produces a difference of the sets') {
+            :Diff = Hoge_fuga_set - Fuga_piyo_set
+            Diff.count.check(1)
+            Diff.has?(Hoge).check(true)
+            Diff.has?(Fuga).check(false)
+            Diff.has?(Piyo).check(false)
+        }
+
+        test('.op_and produces a join of the sets') {
+            :Join = Hoge_fuga_set & Fuga_piyo_set
+            Join.count.check(1)
+            Join.has?(Hoge).check(false)
+            Join.has?(Fuga).check(true)
+            Join.has?(Piyo).check(false)
+        }
+    }
+
+    test('.subset_of? checks if the receiver is a subset of the argument') {
+        set(Hoge).subset_of?(Hoge_fuga_set).check(true)
+        set(Hoge Fuga).subset_of?(Hoge_fuga_set).check(true)
+        set(Hoge Fuga Piyo).subset_of?(Hoge_fuga_set).check(false)
+        set(Hoge Piyo).subset_of?(Hoge_fuga_set).check(false)
+    }
+
+} # }}}1
+
+test_group('set(Hoge Fuga Piyo Hemo)') { # {{{1
+
+    set_up(:Hoge) { value('show' { 'Hoge' }) }
+    set_up(:Fuga) { value('show' { 'Fuga' }) }
+    set_up(:Piyo) { value('show' { 'Piyo' }) }
+    set_up(:Hemo) { value('show' { 'Hemo' }) }
+    set_up(:Heke) { value('show' { 'Heke' }) }
+
+    set_up(:Hoge_fuga_piyo_hemo_set) {
+        set(Hoge Fuga Piyo Hemo)
+    }
+
+    test('.select selects elements which matches with the pattern') {
+        :Selected = Hoge_fuga_piyo_hemo_set.select(or(Hoge Fuga))
+        Selected.count.check(2)
+        Selected.has?(Hoge).check(true)
+        Selected.has?(Fuga).check(true)
+    }
+
+    test('.all? retruns true if a pattern matches with all the elements') {
+        Hoge_fuga_piyo_hemo_set.all?(or(Hoge Fuga Piyo Hemo)).check(true)
+        Hoge_fuga_piyo_hemo_set.all?(or(Hoge)).check(false)
+        Hoge_fuga_piyo_hemo_set.all?(or()).check(false)
+    }
+
+    test('.any retruns true if a pattern matches with at least one element') {
+        Hoge_fuga_piyo_hemo_set.any?(or(Hoge Fuga Piyo Hemo)).check(true)
+        Hoge_fuga_piyo_hemo_set.any?(or(Hoge)).check(true)
+        Hoge_fuga_piyo_hemo_set.any?(or()).check(false)
+    }
+
+} # }}}1
+
+test_group('Equality groups') { # {{{1
+
+    set_up(:Hoge_fuga_group) {
+        [set('Hoge' 'Fuga') set('Hoge' 'Fuga')]
+    }
+
+    set_up(:Hoge_fuga_piyo_group) {
+        [set('Hoge' 'Fuga' 'Piyo') set('Hoge' 'Fuga' 'Piyo')]
+    }
+
+    set_up(:Piyo_hemo_group) {
+        [set('Piyo' 'Hemo') set('Piyo' 'Hemo')]
+    }
+
+    set_up(:Empty_set_group) {
+        :Proto = set.parent
+        [Proto set]
+    }
+
+    test('eqlality constraints') {
+        use('CHECK.check_equality')
+        use('JAVA')
+        check_equality(
+            Hoge_fuga_group
+            Hoge_fuga_piyo_group
+            Piyo_hemo_group
+            Empty_set_group
+            [JAVA.null]
+            [42]
+        )
+    }
+
+} # }}}1