test-array-tests-source.mustache revision 1b7d9ee6f1128c8cb5e16c3a11ba045998296171
42576743851c3c956ad7e867e74df1084c30d434vboxsync<div id="testLogger"></div>
42576743851c3c956ad7e867e74df1084c30d434vboxsync
42576743851c3c956ad7e867e74df1084c30d434vboxsync<script>
42576743851c3c956ad7e867e74df1084c30d434vboxsyncYUI().use('node', 'console', 'test', function (Y) {
42576743851c3c956ad7e867e74df1084c30d434vboxsync
42576743851c3c956ad7e867e74df1084c30d434vboxsync Y.namespace("example.test");
42576743851c3c956ad7e867e74df1084c30d434vboxsync
42576743851c3c956ad7e867e74df1084c30d434vboxsync Y.example.test.ArrayTestCase = new Y.Test.Case({
42576743851c3c956ad7e867e74df1084c30d434vboxsync
42576743851c3c956ad7e867e74df1084c30d434vboxsync //the name of the test case - if not provided, one is automatically generated
42576743851c3c956ad7e867e74df1084c30d434vboxsync name: "Array Tests",
42576743851c3c956ad7e867e74df1084c30d434vboxsync
42576743851c3c956ad7e867e74df1084c30d434vboxsync //-------------------------------------------------------------------------
42576743851c3c956ad7e867e74df1084c30d434vboxsync // Setup and teardown
42576743851c3c956ad7e867e74df1084c30d434vboxsync //-------------------------------------------------------------------------
42576743851c3c956ad7e867e74df1084c30d434vboxsync
42576743851c3c956ad7e867e74df1084c30d434vboxsync /*
42576743851c3c956ad7e867e74df1084c30d434vboxsync * The setUp() method is used to setup data that necessary for a test to
42576743851c3c956ad7e867e74df1084c30d434vboxsync * run. This method is called immediately before each test method is run,
42576743851c3c956ad7e867e74df1084c30d434vboxsync * so it is run as many times as there are test methods.
42576743851c3c956ad7e867e74df1084c30d434vboxsync */
42576743851c3c956ad7e867e74df1084c30d434vboxsync setUp : function () {
42576743851c3c956ad7e867e74df1084c30d434vboxsync this.data = new Array (0,1,2,3,4,5);
42576743851c3c956ad7e867e74df1084c30d434vboxsync },
42576743851c3c956ad7e867e74df1084c30d434vboxsync
42576743851c3c956ad7e867e74df1084c30d434vboxsync /*
42576743851c3c956ad7e867e74df1084c30d434vboxsync * The tearDown() method is used to clean up the initialization that was done
42576743851c3c956ad7e867e74df1084c30d434vboxsync * in the setUp() method. Ideally, it should free up all memory allocated in
42576743851c3c956ad7e867e74df1084c30d434vboxsync * setUp(), anticipating any possible changes to the data. This method is called
42576743851c3c956ad7e867e74df1084c30d434vboxsync * immediately after each test method is run.
42576743851c3c956ad7e867e74df1084c30d434vboxsync */
42576743851c3c956ad7e867e74df1084c30d434vboxsync tearDown : function () {
42576743851c3c956ad7e867e74df1084c30d434vboxsync delete this.data;
42576743851c3c956ad7e867e74df1084c30d434vboxsync },
6998b7cea7c904f33047cd17b05bea760a5839a9vboxsync
02f73b88a6e96b7f1b8ab0bbb98cfb798b566fbdvboxsync //-------------------------------------------------------------------------
42576743851c3c956ad7e867e74df1084c30d434vboxsync // Basic tests - all method names must begin with "test"
cdcfac625bb49f1d4b67aaf8fb8b1cdb69fe49c2vboxsync //-------------------------------------------------------------------------
cdcfac625bb49f1d4b67aaf8fb8b1cdb69fe49c2vboxsync
cdcfac625bb49f1d4b67aaf8fb8b1cdb69fe49c2vboxsync /*
82e3a4017d20f44c30ff909e6b825ff78139cbbbvboxsync * Test the push() method.
82e3a4017d20f44c30ff909e6b825ff78139cbbbvboxsync */
82e3a4017d20f44c30ff909e6b825ff78139cbbbvboxsync testPush : function() {
82e3a4017d20f44c30ff909e6b825ff78139cbbbvboxsync
cdcfac625bb49f1d4b67aaf8fb8b1cdb69fe49c2vboxsync //shortcut variables
82e3a4017d20f44c30ff909e6b825ff78139cbbbvboxsync var ArrayAssert = Y.ArrayAssert;
82e3a4017d20f44c30ff909e6b825ff78139cbbbvboxsync
c6b3d55ef646614cc689faa1fa4ddc961712b6ffvboxsync //do whatever data manipulation is necessary
99a5f7beff5f7bbf1f73923087d60070ddb717d4vboxsync this.data.push(6);
c6b3d55ef646614cc689faa1fa4ddc961712b6ffvboxsync
99a5f7beff5f7bbf1f73923087d60070ddb717d4vboxsync //array-specific assertions
c6b3d55ef646614cc689faa1fa4ddc961712b6ffvboxsync ArrayAssert.isNotEmpty(this.data, "Array should not be empty.");
99a5f7beff5f7bbf1f73923087d60070ddb717d4vboxsync ArrayAssert.contains(6, this.data, "Array should contain 6.");
c6b3d55ef646614cc689faa1fa4ddc961712b6ffvboxsync ArrayAssert.indexOf(6, this.data, 6, "The value in position 6 should be 6.");
c6b3d55ef646614cc689faa1fa4ddc961712b6ffvboxsync
c6b3d55ef646614cc689faa1fa4ddc961712b6ffvboxsync //check that all the values are there
c6b3d55ef646614cc689faa1fa4ddc961712b6ffvboxsync ArrayAssert.itemsAreEqual([0,1,2,3,4,5,6], this.data, "Arrays should be equal.");
c6b3d55ef646614cc689faa1fa4ddc961712b6ffvboxsync
c6b3d55ef646614cc689faa1fa4ddc961712b6ffvboxsync },
99a5f7beff5f7bbf1f73923087d60070ddb717d4vboxsync
99a5f7beff5f7bbf1f73923087d60070ddb717d4vboxsync /*
c6b3d55ef646614cc689faa1fa4ddc961712b6ffvboxsync * Test the pop() method.
c6b3d55ef646614cc689faa1fa4ddc961712b6ffvboxsync */
c6b3d55ef646614cc689faa1fa4ddc961712b6ffvboxsync testPop : function() {
c6b3d55ef646614cc689faa1fa4ddc961712b6ffvboxsync
c6b3d55ef646614cc689faa1fa4ddc961712b6ffvboxsync //shortcut variables
c6b3d55ef646614cc689faa1fa4ddc961712b6ffvboxsync var Assert = Y.Assert;
c6b3d55ef646614cc689faa1fa4ddc961712b6ffvboxsync var ArrayAssert = Y.ArrayAssert;
c6b3d55ef646614cc689faa1fa4ddc961712b6ffvboxsync
43bc5c863e4a7f97ef53171421559ea863af4b11vboxsync //do whatever data manipulation is necessary
cdcfac625bb49f1d4b67aaf8fb8b1cdb69fe49c2vboxsync var value = this.data.pop();
cdcfac625bb49f1d4b67aaf8fb8b1cdb69fe49c2vboxsync
cdcfac625bb49f1d4b67aaf8fb8b1cdb69fe49c2vboxsync //array shouldn't be empty
42576743851c3c956ad7e867e74df1084c30d434vboxsync ArrayAssert.isNotEmpty(this.data, "Array should not be empty.");
42576743851c3c956ad7e867e74df1084c30d434vboxsync
99a5f7beff5f7bbf1f73923087d60070ddb717d4vboxsync //basic equality assertion - expected value, actual value, optional error message
99a5f7beff5f7bbf1f73923087d60070ddb717d4vboxsync Assert.areEqual(5, this.data.length, "Array should have 5 items.");
43bc5c863e4a7f97ef53171421559ea863af4b11vboxsync Assert.areEqual(5, value, "Value should be 5.");
99a5f7beff5f7bbf1f73923087d60070ddb717d4vboxsync
43bc5c863e4a7f97ef53171421559ea863af4b11vboxsync ArrayAssert.itemsAreSame([0,1,2,3,4], this.data, "Arrays should be equal.");
43bc5c863e4a7f97ef53171421559ea863af4b11vboxsync },
43bc5c863e4a7f97ef53171421559ea863af4b11vboxsync
99a5f7beff5f7bbf1f73923087d60070ddb717d4vboxsync /*
43bc5c863e4a7f97ef53171421559ea863af4b11vboxsync * Test the reverse() method.
43bc5c863e4a7f97ef53171421559ea863af4b11vboxsync */
43bc5c863e4a7f97ef53171421559ea863af4b11vboxsync testReverse : function() {
02f73b88a6e96b7f1b8ab0bbb98cfb798b566fbdvboxsync
02f73b88a6e96b7f1b8ab0bbb98cfb798b566fbdvboxsync //shortcut variables
02f73b88a6e96b7f1b8ab0bbb98cfb798b566fbdvboxsync var ArrayAssert = Y.ArrayAssert;
cdcfac625bb49f1d4b67aaf8fb8b1cdb69fe49c2vboxsync
cdcfac625bb49f1d4b67aaf8fb8b1cdb69fe49c2vboxsync //do whatever data manipulation is necessary
43d42414bba4afb4e2aa2565d9278a3194f09304vboxsync this.data = this.data.reverse();
43d42414bba4afb4e2aa2565d9278a3194f09304vboxsync
cdcfac625bb49f1d4b67aaf8fb8b1cdb69fe49c2vboxsync ArrayAssert.itemsAreEqual([5,4,3,2,1,0], this.data, "Arrays should be equal.");
43d42414bba4afb4e2aa2565d9278a3194f09304vboxsync },
43d42414bba4afb4e2aa2565d9278a3194f09304vboxsync
43d42414bba4afb4e2aa2565d9278a3194f09304vboxsync /*
cdcfac625bb49f1d4b67aaf8fb8b1cdb69fe49c2vboxsync * Test the shift() method.
cdcfac625bb49f1d4b67aaf8fb8b1cdb69fe49c2vboxsync */
cdcfac625bb49f1d4b67aaf8fb8b1cdb69fe49c2vboxsync testShift : function() {
cdcfac625bb49f1d4b67aaf8fb8b1cdb69fe49c2vboxsync
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync //shortcut variables
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync var Assert = Y.Assert;
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync var ArrayAssert = Y.ArrayAssert;
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync //do whatever data manipulation is necessary
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync var value = this.data.shift();
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync //array shouldn't be empty
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync ArrayAssert.isNotEmpty(this.data, "Array should not be empty.");
42576743851c3c956ad7e867e74df1084c30d434vboxsync
1379dfd407ada5fab15655776896f13b61a951fdvboxsync //basic equality assertion - expected value, actual value, optional error message
1379dfd407ada5fab15655776896f13b61a951fdvboxsync Assert.areEqual(5, this.data.length, "Array should have 6 items.");
1379dfd407ada5fab15655776896f13b61a951fdvboxsync Assert.areEqual(0, value, "Value should be 0.");
1379dfd407ada5fab15655776896f13b61a951fdvboxsync
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync ArrayAssert.itemsAreEqual([1,2,3,4,5], this.data, "Arrays should be equal.");
1379dfd407ada5fab15655776896f13b61a951fdvboxsync },
1379dfd407ada5fab15655776896f13b61a951fdvboxsync
1379dfd407ada5fab15655776896f13b61a951fdvboxsync /*
1379dfd407ada5fab15655776896f13b61a951fdvboxsync * Test the splice() method.
1379dfd407ada5fab15655776896f13b61a951fdvboxsync */
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync testSplice : function() {
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync //shortcut variables
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync var Assert = Y.Assert;
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync var ArrayAssert = Y.ArrayAssert;
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync //do whatever data manipulation is necessary
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync var removed = this.data.splice(1, 2, 99, 100);
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync //basic equality assertion - expected value, actual value, optional error message
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync Assert.areEqual(6, this.data.length, "Array should have 6 items.");
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync //the new items should be there
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync ArrayAssert.indexOf(99, this.data, 1, "Value at index 1 should be 99.");
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync ArrayAssert.indexOf(100, this.data, 2, "Value at index 2 should be 100.");
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync ArrayAssert.itemsAreEqual([0,99,100,3,4,5], this.data, "Arrays should be equal.");
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync ArrayAssert.itemsAreEqual([1,2], removed, "Removed values should be an array containing 1 and 2.");
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync },
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync
1379dfd407ada5fab15655776896f13b61a951fdvboxsync /*
1379dfd407ada5fab15655776896f13b61a951fdvboxsync * Test the unshift() method.
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync */
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync testUnshift : function() {
1379dfd407ada5fab15655776896f13b61a951fdvboxsync
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync //shortcut variables
1379dfd407ada5fab15655776896f13b61a951fdvboxsync var Assert = Y.Assert;
e89bf42607ac3c1b6c60fabbf6067a38adb1284bvboxsync var ArrayAssert = Y.ArrayAssert;
e89bf42607ac3c1b6c60fabbf6067a38adb1284bvboxsync
e89bf42607ac3c1b6c60fabbf6067a38adb1284bvboxsync //do whatever data manipulation is necessary
e89bf42607ac3c1b6c60fabbf6067a38adb1284bvboxsync this.data.unshift(-1);
1379dfd407ada5fab15655776896f13b61a951fdvboxsync
1379dfd407ada5fab15655776896f13b61a951fdvboxsync //basic equality assertion - expected value, actual value, optional error message
1379dfd407ada5fab15655776896f13b61a951fdvboxsync Assert.areEqual(7, this.data.length, "Array should have 7 items.");
1379dfd407ada5fab15655776896f13b61a951fdvboxsync
1379dfd407ada5fab15655776896f13b61a951fdvboxsync //the new item should be there
1379dfd407ada5fab15655776896f13b61a951fdvboxsync ArrayAssert.indexOf(-1, this.data, 0, "First item should be -1.");
1379dfd407ada5fab15655776896f13b61a951fdvboxsync
1379dfd407ada5fab15655776896f13b61a951fdvboxsync ArrayAssert.itemsAreEqual([-1,0,1,2,3,4,5], this.data, "Arrays should be equal.");
1379dfd407ada5fab15655776896f13b61a951fdvboxsync }
1379dfd407ada5fab15655776896f13b61a951fdvboxsync
1379dfd407ada5fab15655776896f13b61a951fdvboxsync });
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync //create the console
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync var r = new Y.Console({
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync newestOnTop : false,
1379dfd407ada5fab15655776896f13b61a951fdvboxsync style: 'block' // to anchor in the example content
1379dfd407ada5fab15655776896f13b61a951fdvboxsync });
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync
1379dfd407ada5fab15655776896f13b61a951fdvboxsync r.render('#testLogger');
1379dfd407ada5fab15655776896f13b61a951fdvboxsync
1379dfd407ada5fab15655776896f13b61a951fdvboxsync Y.Test.Runner.add(Y.example.test.ArrayTestCase);
2d66abeefb9716ed570bb5714884d3fe08629452vboxsync
1379dfd407ada5fab15655776896f13b61a951fdvboxsync //run the tests
1379dfd407ada5fab15655776896f13b61a951fdvboxsync Y.Test.Runner.run();
1379dfd407ada5fab15655776896f13b61a951fdvboxsync});
1379dfd407ada5fab15655776896f13b61a951fdvboxsync
1379dfd407ada5fab15655776896f13b61a951fdvboxsync</script>
1379dfd407ada5fab15655776896f13b61a951fdvboxsync