File: traits.test

package info (click to toggle)
nsf 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,208 kB
  • sloc: ansic: 32,687; tcl: 10,723; sh: 660; pascal: 176; javascript: 135; lisp: 41; makefile: 24
file content (68 lines) | stat: -rw-r--r-- 1,794 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package prefer latest

package require nx::trait
package require nx::test

nx::test case basics {

  nx::Trait create t1 {
    :public method t1m1 {} {return t1.[current method]}
    :public method t1m2 {} {return t1.[current method]}

    # This trait requires a method "foo" 
    :requiredMethods foo
  }
  nx::Trait create t2 {
    :public method "bar x" {} {return t2.[current methodpath]}
    :public method "bar y" {} {return t2.[current methodpath]}
    :public method foo {} {return t2.[current methodpath]}

    # This trait requires a method "t1m1" 
    :requiredMethods t1m1
  }

  nx::Trait create t3 {
    :public method "bar y" {} {return t3.[current methodpath]}
    :public method "bar z" {} {return t3.[current methodpath]}
  }

  nx::Class create C {
    :property {collection ""}
    :public method foo {} {return [current method]}
    :create c1
  }

  ? {c1 foo} "foo"

  ? {C require trait t2} "trait t2 requires t1m1, which is not defined for ::C"

  ? {lsort [C info methods]} "foo"
  C require trait t1
  ? {lsort [C info methods]} "foo t1m1 t1m2"
  ? {c1 foo} "foo"

  ? {C require trait t2} ""
  ? {lsort [C info methods]} "bar foo t1m1 t1m2"
  ? {lsort [C info methods -path]} "{bar x} {bar y} foo t1m1 t1m2"
  # trait t2 redefines t2, so we call that see its result here
  ? {c1 foo} "t2.foo"
  ? {c1 bar x} "t2.bar x"
  ? {c1 bar y} "t2.bar y"

  ? {C require trait t3} ""
  ? {lsort [C info methods]} "bar foo t1m1 t1m2"
  ? {lsort [C info methods -path]} "{bar x} {bar y} {bar z} foo t1m1 t1m2"
  # trait t3 redefines "bar y", so we call that see its result here
  ? {c1 foo} "t2.foo"
  ? {c1 bar x} "t2.bar x"
  ? {c1 bar y} "t3.bar y"
  ? {c1 bar z} "t3.bar z"
}


#
# Local variables:
#    mode: tcl
#    tcl-indent-level: 2
#    indent-tabs-mode: nil
# End: