|
| 1 | +/** |
| 2 | + * Copyright (c) 2015, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, it } from 'mocha'; |
| 11 | +import { expectPassesRule, expectFailsRule } from './harness'; |
| 12 | +import { |
| 13 | + UniqueDirectivesPerLocation, |
| 14 | + duplicateDirectiveMessage, |
| 15 | +} from '../rules/UniqueDirectivesPerLocation'; |
| 16 | + |
| 17 | + |
| 18 | +function duplicateDirective(directiveName, l1, c1, l2, c2) { |
| 19 | + return { |
| 20 | + message: duplicateDirectiveMessage(directiveName), |
| 21 | + locations: [ { line: l1, column: c1 }, { line: l2, column: c2 } ], |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +describe('Validate: Directives Are Unique Per Location', () => { |
| 26 | + |
| 27 | + it('no directives', () => { |
| 28 | + expectPassesRule(UniqueDirectivesPerLocation, ` |
| 29 | + fragment Test on Type { |
| 30 | + field |
| 31 | + } |
| 32 | + `); |
| 33 | + }); |
| 34 | + |
| 35 | + it('unique directives in different locations', () => { |
| 36 | + expectPassesRule(UniqueDirectivesPerLocation, ` |
| 37 | + fragment Test on Type @directiveA { |
| 38 | + field @directiveB |
| 39 | + } |
| 40 | + `); |
| 41 | + }); |
| 42 | + |
| 43 | + it('unique directives in same locations', () => { |
| 44 | + expectPassesRule(UniqueDirectivesPerLocation, ` |
| 45 | + fragment Test on Type @directiveA @directiveB { |
| 46 | + field @directiveA @directiveB |
| 47 | + } |
| 48 | + `); |
| 49 | + }); |
| 50 | + |
| 51 | + it('same directives in different locations', () => { |
| 52 | + expectPassesRule(UniqueDirectivesPerLocation, ` |
| 53 | + fragment Test on Type @directiveA { |
| 54 | + field @directiveA |
| 55 | + } |
| 56 | + `); |
| 57 | + }); |
| 58 | + |
| 59 | + it('same directives in similar locations', () => { |
| 60 | + expectPassesRule(UniqueDirectivesPerLocation, ` |
| 61 | + fragment Test on Type { |
| 62 | + field @directive |
| 63 | + field @directive |
| 64 | + } |
| 65 | + `); |
| 66 | + }); |
| 67 | + |
| 68 | + it('duplicate directives in one location', () => { |
| 69 | + expectFailsRule(UniqueDirectivesPerLocation, ` |
| 70 | + fragment Test on Type { |
| 71 | + field @directive @directive |
| 72 | + } |
| 73 | + `, [ |
| 74 | + duplicateDirective('directive', 3, 15, 3, 26) |
| 75 | + ]); |
| 76 | + }); |
| 77 | + |
| 78 | + it('many duplicate directives in one location', () => { |
| 79 | + expectFailsRule(UniqueDirectivesPerLocation, ` |
| 80 | + fragment Test on Type { |
| 81 | + field @directive @directive @directive |
| 82 | + } |
| 83 | + `, [ |
| 84 | + duplicateDirective('directive', 3, 15, 3, 26), |
| 85 | + duplicateDirective('directive', 3, 15, 3, 37) |
| 86 | + ]); |
| 87 | + }); |
| 88 | + |
| 89 | + it('different duplicate directives in one location', () => { |
| 90 | + expectFailsRule(UniqueDirectivesPerLocation, ` |
| 91 | + fragment Test on Type { |
| 92 | + field @directiveA @directiveB @directiveA @directiveB |
| 93 | + } |
| 94 | + `, [ |
| 95 | + duplicateDirective('directiveA', 3, 15, 3, 39), |
| 96 | + duplicateDirective('directiveB', 3, 27, 3, 51) |
| 97 | + ]); |
| 98 | + }); |
| 99 | + |
| 100 | + it('duplicate directives in many locations', () => { |
| 101 | + expectFailsRule(UniqueDirectivesPerLocation, ` |
| 102 | + fragment Test on Type @directive @directive { |
| 103 | + field @directive @directive |
| 104 | + } |
| 105 | + `, [ |
| 106 | + duplicateDirective('directive', 2, 29, 2, 40), |
| 107 | + duplicateDirective('directive', 3, 15, 3, 26) |
| 108 | + ]); |
| 109 | + }); |
| 110 | + |
| 111 | +}); |
0 commit comments