diff --git a/controller.js b/controller.js index 9354734..2de2ccf 100644 --- a/controller.js +++ b/controller.js @@ -1,4 +1,4 @@ -angular.module('test', []) +angular.module('test', ['test.directives', 'test.filters']) .controller('testController', ['$scope', function($scope) { }]); \ No newline at end of file diff --git a/directive.js b/directive.js new file mode 100644 index 0000000..7d74784 --- /dev/null +++ b/directive.js @@ -0,0 +1,26 @@ +angular.module('test.directives', []) +.directive('numbers', function() { + return { + restrict: 'E', + scope: { + from: '&', + to: '&', + filter: '&', + position: '&' + }, + controller: ['$scope', '$filter', function($scope, $filter) { + $scope.numbers = function(from, to, filterName) { + var numbers = []; + for (var i = from; i <= to; i++) { + if (!filterName || $filter(filterName)(i)) { + numbers.push(i); + } + } + return numbers; + }; + }], + template: '
\ +
{{number}}
\ +
' + }; +}); \ No newline at end of file diff --git a/filters.js b/filters.js new file mode 100644 index 0000000..160985e --- /dev/null +++ b/filters.js @@ -0,0 +1,16 @@ +angular.module('test.filters', []) +.filter('divisibleBy', function() { + return function(dividend, divisor) { + return dividend % divisor === 0; + }; +}) +.filter('even', ['$filter', function($filter) { + return function(number) { + return $filter('divisibleBy')(number, 2); + }; +}]) +.filter('odd', ['$filter', function($filter) { + return function(number) { + return !$filter('even')(number); + }; +}]); \ No newline at end of file diff --git a/index.html b/index.html index 4079a43..0f320b9 100644 --- a/index.html +++ b/index.html @@ -3,30 +3,35 @@ + +
- - + +
- - - + + +
- - + +
- +
diff --git a/styles.css b/styles.css index 49ac741..ac876a1 100644 --- a/styles.css +++ b/styles.css @@ -26,4 +26,12 @@ html, body { .buttons-holder { float: right; +} + +.pull-right { + float: right; +} + +.pull-left { + float: left; } \ No newline at end of file diff --git a/tests/directives/numbers.js b/tests/directives/numbers.js new file mode 100644 index 0000000..4021618 --- /dev/null +++ b/tests/directives/numbers.js @@ -0,0 +1,56 @@ +describe('numbers directive test', function() { + var $compile, + $rootScope; + + beforeEach(function() { + module('test'); + + module(function($provide) { + $provide.service('$filter', function() { + return function(filterName) { + if (filterName !== 'testFilter') return null; + + return function(number) { + return number !== 4; + }; + }; + }); + }); + + inject(function(_$compile_, _$rootScope_) { + $compile = _$compile_; + $rootScope = _$rootScope_; + }); + }); + + it('should add the list of numbers', function() { + var element = $compile("")($rootScope); + + $rootScope.$digest(); + + expect(element.html()).toContain("3"); + expect(element.html()).toContain("4"); + expect(element.html()).toContain("5"); + + expect(element.html()).not.toContain("2"); + expect(element.html()).not.toContain("6"); + }); + + it('should filter the list of numbers', function() { + var element = $compile("")($rootScope); + + $rootScope.$digest(); + + expect(element.html()).toContain("3"); + expect(element.html()).not.toContain("4"); + expect(element.html()).toContain("5"); + }); + + it('should add positioning classes', function() { + var element = $compile("")($rootScope); + + $rootScope.$digest(); + + expect(element.find('div').eq(0).hasClass('pull-right')).toBe(true); + }); +}); \ No newline at end of file diff --git a/tests/filters/divisibleBy.js b/tests/filters/divisibleBy.js new file mode 100644 index 0000000..effc585 --- /dev/null +++ b/tests/filters/divisibleBy.js @@ -0,0 +1,23 @@ +describe('divisibleBy filter test', function() { + 'use strict'; + + var filter; + + beforeEach(function() { + module('test'); + + inject(function($filter) { + filter = $filter('divisibleBy'); + }); + }); + + it('should succeed when given natural divisors', function() { + expect(filter(6,2)).toBe(true); + expect(filter(6,3)).toBe(true); + }); + + it('should fail when given not natural divisors', function() { + expect(filter(6,4)).toBe(false); + expect(filter(6,5)).toBe(false); + }); +}); \ No newline at end of file diff --git a/tests/filters/even.js b/tests/filters/even.js new file mode 100644 index 0000000..39dfe89 --- /dev/null +++ b/tests/filters/even.js @@ -0,0 +1,23 @@ +describe('even filter test', function() { + 'use strict'; + + var filter; + + beforeEach(function() { + module('test'); + + inject(function($filter) { + filter = $filter('even'); + }); + }); + + it('should fail unless given multiples of 2', function() { + expect(filter(7)).toBe(false); + expect(filter(9)).toBe(false); + }); + + it('should succeed when given multiples of 2', function() { + expect(filter(6)).toBe(true); + expect(filter(8)).toBe(true); + }); +}); \ No newline at end of file diff --git a/tests/filters/odd.js b/tests/filters/odd.js new file mode 100644 index 0000000..20f1d96 --- /dev/null +++ b/tests/filters/odd.js @@ -0,0 +1,23 @@ +describe('odd filter test', function() { + 'use strict'; + + var filter; + + beforeEach(function() { + module('test'); + + inject(function($filter) { + filter = $filter('odd'); + }); + }); + + it('should succeed unless given multiples of 2', function() { + expect(filter(7)).toBe(true); + expect(filter(9)).toBe(true); + }); + + it('should fail when given multiples of 2', function() { + expect(filter(6)).toBe(false); + expect(filter(8)).toBe(false); + }); +}); \ No newline at end of file