|
| 1 | +package scala.xml |
| 2 | + |
| 3 | +import org.scalacheck.Prop |
| 4 | +import org.scalacheck.{ Properties => PropertiesFor } |
| 5 | +import org.scalacheck.Prop.AnyOperators |
| 6 | +import org.scalacheck.Prop.BooleanOperators |
| 7 | + |
| 8 | +object AttributeSpec extends PropertiesFor("Attribute") |
| 9 | + with AttributeGen |
| 10 | + with NamespaceBindingGen |
| 11 | + with NodeGen |
| 12 | + with MetaDataGen { |
| 13 | + |
| 14 | + property("canEqual(this)") = { |
| 15 | + Prop.forAll { a: Attribute => |
| 16 | + a.canEqual(a) |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + property("apply") = { |
| 21 | + Prop.forAll { (a: Attribute, s: String) => |
| 22 | + (!s.isEmpty && Utility.isNameStart(s(0))) ==> |
| 23 | + (a(s) != s) |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + property("apply(key)") = { |
| 28 | + Prop.forAll { a: Attribute => |
| 29 | + Prop.iff[Attribute](a, { |
| 30 | + case p: PrefixedAttribute => |
| 31 | + Prop.passed |
| 32 | + case u: UnprefixedAttribute if u.key ne null => |
| 33 | + u(u.key) ?= u.value |
| 34 | + case _ => Prop.falsified |
| 35 | + }) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + property("apply(uri, namespace, key)") = { |
| 40 | + Prop.forAll { a: Attribute => |
| 41 | + Prop.iff[Attribute](a, { |
| 42 | + case p: PrefixedAttribute => |
| 43 | + val res = p(p.key, NamespaceBinding(p.pre, p.key, TopScope), p.key) |
| 44 | + res ?= p.value |
| 45 | + case u: UnprefixedAttribute => |
| 46 | + Prop.passed |
| 47 | + case _ => Prop.falsified |
| 48 | + }) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + property("isPrefixed") = { |
| 53 | + Prop.forAll { a: Attribute => |
| 54 | + Prop.iff[Attribute](a, { |
| 55 | + case p: PrefixedAttribute => |
| 56 | + a.isPrefixed ?= true |
| 57 | + case u: UnprefixedAttribute => |
| 58 | + a.isPrefixed ?= false |
| 59 | + case _ => Prop.falsified |
| 60 | + }) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + property("length") = { |
| 65 | + Prop.forAll { a: Attribute => |
| 66 | + a.length >= 0 |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + property("size") = { |
| 71 | + Prop.forAll { a: Attribute => |
| 72 | + a.size >= 0 |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + property("toString") = { |
| 77 | + Prop.forAll { a: Attribute => |
| 78 | + val str = a.toString |
| 79 | + Prop.iff[Attribute](a, { |
| 80 | + case a: PrefixedAttribute if a.hasNext => |
| 81 | + str ?= s""" ${a.pre}:${a.key}="${a.value}"${a.next}""" |
| 82 | + case a: UnprefixedAttribute if a.hasNext => |
| 83 | + str ?= s""" ${a.key}="${a.value}"${a.next}""" |
| 84 | + case a: PrefixedAttribute => |
| 85 | + str ?= s""" ${a.pre}:${a.key}="${a.value}"""" |
| 86 | + case a: UnprefixedAttribute => |
| 87 | + str ?= s""" ${a.key}="${a.value}"""" |
| 88 | + case _ => Prop.falsified |
| 89 | + }) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + property("unapply") = { |
| 94 | + Prop.forAll { a: Attribute => |
| 95 | + Attribute.unapply(a) ?= Some((a.key, a.value, a.next)) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + property("remove(key)") = { |
| 100 | + Prop.forAll { a: Attribute => |
| 101 | + Prop.iff[Attribute](a, { |
| 102 | + case a: PrefixedAttribute => |
| 103 | + a.remove(a.key) ?= a.copy(a.next.remove(a.key)) // FIXME: ??? |
| 104 | + case a: UnprefixedAttribute => |
| 105 | + a.remove(a.key) ?= a.next |
| 106 | + case _ => Prop.falsified |
| 107 | + }) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + property("remove") = { |
| 112 | + Prop.forAll { (a: Attribute, s: String) => |
| 113 | + s != a.key ==> |
| 114 | + (a.remove(s) ?= a.copy(a.next.remove(s))) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + property("getNamespace") = { |
| 119 | + Prop.forAll { (a: Attribute, n: Node) => |
| 120 | + a.getNamespace(n) ?= n.getNamespace(a.pre) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + property("wellformed") = { |
| 125 | + Prop.forAll { (a: Attribute, scope: NamespaceBinding) => |
| 126 | + Prop.iff[Attribute](a, { |
| 127 | + case a: PrefixedAttribute => |
| 128 | + val res = a.wellformed(scope) |
| 129 | + res ?= ((null eq a.next(scope.getURI(a.pre), scope, a.key)) && a.next.wellformed(scope)) |
| 130 | + case a: UnprefixedAttribute => |
| 131 | + val res = a.wellformed(scope) |
| 132 | + res ?= ((null eq a.next(null, scope, a.key)) && a.next.wellformed(scope)) |
| 133 | + case _ => Prop.falsified |
| 134 | + }) |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments