summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Test.AnotherWay.geom_eq.js
blob: 893c5b5a2f1d12bdcc8f7096c0bf5021123ed056 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
 * File: Test.AnotherWay.geom_eq.js
 * Adds a geom_eq method to AnotherWay test objects.
 *
 */

(function() {
    
    /**
     * Function assertEqual
     * Test two objects for equivalence (based on ==).  Throw an exception
     *     if not equivalent.
     * 
     * Parameters:
     * got - {Object}
     * expected - {Object}
     * msg - {String} The message to be thrown.  This message will be appended
     *     with ": got {got} but expected {expected}" where got and expected are
     *     replaced with string representations of the above arguments.
     */
    function assertEqual(got, expected, msg) {
        if(got === undefined) {
            got = "undefined";
        } else if (got === null) {
            got = "null";
        }
        if(expected === undefined) {
            expected = "undefined";
        } else if (expected === null) {
            expected = "null";
        }
        if(got != expected) {
            throw msg + ": got '" + got + "' but expected '" + expected + "'";
        }
    }
    
    /**
     * Function assertFloatEqual
     * Test two objects for floating point equivalence.  Throw an exception
     *     if not equivalent.
     * 
     * Parameters:
     * got - {Object}
     * expected - {Object}
     * msg - {String} The message to be thrown.  This message will be appended
     *     with ": got {got} but expected {expected}" where got and expected are
     *     replaced with string representations of the above arguments.
     */
    function assertFloatEqual(got, expected, msg) {
        var OpenLayers = Test.AnotherWay._g_test_iframe.OpenLayers;
        if(got === undefined) {
            got = "undefined";
        } else if (got === null) {
            got = "null";
        }
        if(expected === undefined) {
            expected = "undefined";
        } else if (expected === null) {
            expected = "null";
        }
        if(Math.abs(got - expected) > Math.pow(10, -OpenLayers.Util.DEFAULT_PRECISION)) {
            throw msg + ": got '" + got + "' but expected '" + expected + "'";
        }
    }
    
    /**
     * Function assertGeometryEqual
     * Test two geometries for equivalence.  Geometries are considered
     *     equivalent if they are of the same class, and given component
     *     geometries, if all components are equivalent. Throws a message as
     *     exception if not equivalent.
     * 
     * Parameters:
     * got - {OpenLayers.Geometry}
     * expected - {OpenLayers.Geometry}
     * options - {Object} Optional object for configuring test options.
     */
    function assertGeometryEqual(got, expected, options) {
        
        var OpenLayers = Test.AnotherWay._g_test_iframe.OpenLayers;

        // compare types
        assertEqual(typeof got, typeof expected, "Object types mismatch");
        
        // compare classes
        assertEqual(got.CLASS_NAME, expected.CLASS_NAME, "Object class mismatch");
        
        if(got instanceof OpenLayers.Geometry.Point) {
            // compare points
            assertFloatEqual(got.x, expected.x, "x mismatch");
            assertFloatEqual(got.y, expected.y, "y mismatch");
            assertFloatEqual(got.z, expected.z, "z mismatch");
        } else {
            // compare components
            assertEqual(
                got.components.length, expected.components.length,
                "Component length mismatch for " + got.CLASS_NAME
            );
            for(var i=0; i<got.components.length; ++i) {
                try {
                    assertGeometryEqual(
                        got.components[i], expected.components[i], options
                    );
                } catch(err) {
                    throw "Bad component " + i + " for " + got.CLASS_NAME + ": " + err;
                }
            }
        }
        return true;
    }
    
    /**
     * Function: Test.AnotherWay._test_object_t.geom_eq
     * Test if two geometry objects are equivalent.  Tests for same geometry
     *     class, same number of components (if any), equivalent component
     *     geometries, and same coordinates.
     *
     * (code)
     * t.geom_eq(got, expected, message);
     * (end)
     * 
     * Parameters:
     * got - {OpenLayers.Geometry} Any geometry instance.
     * expected - {OpenLayers.Geometry} The expected geometry.
     * msg - {String} A message to print with test output.
     * options - {Object} Optional object for configuring test options.
     */
    var proto = Test.AnotherWay._test_object_t.prototype;
    proto.geom_eq = function(got, expected, msg, options) {        
        // test geometries for equivalence
        try {
            assertGeometryEqual(got, expected, options);
            this.ok(true, msg);
        } catch(err) {
            this.fail(msg + ": " + err);
        }
    }
    
})();