|
| 1 | +package metro.repository |
| 2 | + |
| 3 | +import metro.model.Station |
| 4 | +import metro.model.StationId |
| 5 | +import spock.lang.Specification |
| 6 | +import spock.lang.Subject |
| 7 | +import spock.lang.Title |
| 8 | + |
| 9 | +@Title("Command 'getGraph' specifications") |
| 10 | +class RepositoryGraphSpec extends Specification { |
| 11 | + @Subject |
| 12 | + def repository = new MetroRepositoryJson() |
| 13 | + |
| 14 | + def 'should create an empty graph from the empty metro schema'() { |
| 15 | + given: 'an empty metro map' |
| 16 | + repository.metroMap = [:] |
| 17 | + |
| 18 | + when: 'we create a graph from the metro map' |
| 19 | + def graph = repository.getGraph() |
| 20 | + |
| 21 | + then: 'we get an empty graph' |
| 22 | + graph.schema().isEmpty() |
| 23 | + } |
| 24 | + |
| 25 | + def 'should create a graph from one line, one station metro schema'() { |
| 26 | + given: 'a one line, one station metro schema' |
| 27 | + repository.metroMap = [(line): [new Station(station, 5)] as ArrayDeque] |
| 28 | + |
| 29 | + when: 'we create a graph from the metro map' |
| 30 | + def graph = repository.getGraph() |
| 31 | + |
| 32 | + then: 'the graph schema is not empty' |
| 33 | + graph.schema() |
| 34 | + |
| 35 | + and: 'it has only one vertex' |
| 36 | + graph.schema().size() == 1 |
| 37 | + |
| 38 | + and: 'the only vertex in the graph has correct Station ID' |
| 39 | + graph.schema().keySet().contains new StationId(line, station) |
| 40 | + |
| 41 | + where: 'metro line and metro station as' |
| 42 | + line | station |
| 43 | + 'Bakerloo line' | 'Waterloo' |
| 44 | + 'Central line' | 'White City' |
| 45 | + } |
| 46 | + |
| 47 | + def 'should create a graph from one line and three stations'() { |
| 48 | + given: 'three stations creating a open-ended line' |
| 49 | + def A1 = new Station('A1', 7, ['A2'] as Set, [] as Set, [] as Set) |
| 50 | + def A2 = new Station('A2', 5, ['A3'] as Set, ['A1'] as Set, [] as Set) |
| 51 | + def A3 = new Station('A3', 9, [] as Set, ['A2'] as Set, [] as Set) |
| 52 | + |
| 53 | + and: 'a metro line with these stations' |
| 54 | + def L1 = [A1, A2, A3] as ArrayDeque |
| 55 | + |
| 56 | + and: 'a metro schema with one line' |
| 57 | + repository.metroMap = [one: L1] |
| 58 | + |
| 59 | + when: 'we create a graph from the metro map' |
| 60 | + def graph = repository.getGraph() |
| 61 | + |
| 62 | + then: 'the graph schema is not empty' |
| 63 | + graph.schema() |
| 64 | + |
| 65 | + and: 'it has three vertexes' |
| 66 | + graph.schema().size() == 3 |
| 67 | + } |
| 68 | + |
| 69 | + StationId sid(line, name) { |
| 70 | + new StationId(line, name) |
| 71 | + } |
| 72 | +} |
0 commit comments