1/*
2 * Copyright 2001-2008 Artima, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.scalatest.matchers
17
18import org.scalatest._
19
20class PropertyFunSuite extends FunSuite with ShouldMatchers {
21
22  test("object has no appropriately named field, method, or get method (0, 0, 0)") {
23    class DontGotNuthin
24    val dgn = new DontGotNuthin
25    val result = Helper.accessProperty(dgn, 'fred, false)
26    result should be (None)
27  }
28
29  test("object has only an appropriately named get method (0, 0, 1)") {
30    class HasGetMethod {
31      def getCow: Int = 1
32    }
33    val obj = new HasGetMethod
34    val result = Helper.accessProperty(obj, 'cow, false)
35    result should be (Some(1))
36  }
37
38  test("object has only an appropriately named Boolean is method (0, 0, 1)") {
39    class HasIsMethod {
40      def isCow: Boolean = true
41    }
42    val obj = new HasIsMethod
43    val result = Helper.accessProperty(obj, 'cow, true)
44    result should be (Some(true))
45  }
46
47  test("object has only an appropriately named method (0, 1, 0)") {
48    class HasMethod {
49      def cow: Int = 1
50    }
51    val obj = new HasMethod
52    val result = Helper.accessProperty(obj, 'cow, false)
53    result should be (Some(1))
54  }
55
56  test("object has only an appropriately named Scala field, which results in a Java method (0, 1, 0)") {
57    class HasScalaField {
58      val cow: Int = 1
59    }
60    val obj = new HasScalaField
61    val result = Helper.accessProperty(obj, 'cow, false)
62    result should be (Some(1))
63  }
64
65  test("object has only an appropriately named method and getMethod (0, 1, 1)") {
66    class HasMethod {
67      def cow: Int = 1
68      def getCow: Int = 2
69    }
70    val obj = new HasMethod
71    val result = Helper.accessProperty(obj, 'cow, false)
72    result should be (Some(1))
73  }
74
75  test("object has only an appropriately named field (1, 0, 0)") {
76    val obj = new HasField // A Java class, because can't get a field in a Scala class
77    val result = Helper.accessProperty(obj, 'cow, false)
78    result should be (Some(1))
79  }
80
81  test("object has an appropriately named field and getMethod (1, 0, 1)") {
82    val obj = new HasFieldAndGetMethod // A Java class, because can't get a field in a Scala class
83    val result = Helper.accessProperty(obj, 'cow, false)
84    result should be (Some(1))
85  }
86
87  test("object has an appropriately named field and method (1, 1, 0)") {
88    val obj = new HasFieldAndMethod // A Java class, because can't get a field in a Scala class
89    val result = Helper.accessProperty(obj, 'cow, false)
90    result should be (Some(1))
91  }
92
93  test("object has an appropriately named field and method and getMethod (1, 1, 1)") {
94    val obj = new HasFieldMethodAndGetMethod // A Java class, because can't get a field in a Scala class
95    val result = Helper.accessProperty(obj, 'cow, false)
96    result should be (Some(1))
97  }
98
99  test("works on set.empty") {
100    val result1 = Helper.accessProperty(Set(), 'empty, true)
101    result1 should be (Some(true))
102    val result2 = Helper.accessProperty(Set(1, 2, 3), 'empty, true)
103    result2 should be (Some(false))
104  }
105}
106