|
| 1 | +/* |
| 2 | + * Copyright 2019 the original authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.github.hauner.openapi.gradle |
| 18 | + |
| 19 | +import com.github.hauner.openapi.gradle.support.Sl4jMockRule |
| 20 | +import org.junit.Rule |
| 21 | +import org.slf4j.Logger |
| 22 | +import spock.lang.Specification |
| 23 | + |
| 24 | +class MapBuilderSpec extends Specification { |
| 25 | + |
| 26 | + def log = Mock Logger |
| 27 | + @Rule Sl4jMockRule rule = new Sl4jMockRule(MapBuilder, log) |
| 28 | + |
| 29 | + void "does set simple property value via method" () { |
| 30 | + def builder = new MapBuilder() |
| 31 | + |
| 32 | + when: |
| 33 | + builder.with { |
| 34 | + prop "value" |
| 35 | + } |
| 36 | + |
| 37 | + def result = builder.get() |
| 38 | + |
| 39 | + then: |
| 40 | + result.prop == "value" |
| 41 | + } |
| 42 | + |
| 43 | + void "does set simple property value via set" () { |
| 44 | + def builder = new MapBuilder() |
| 45 | + |
| 46 | + when: |
| 47 | + builder.with { |
| 48 | + prop = "value" |
| 49 | + } |
| 50 | + |
| 51 | + def result = builder.get() |
| 52 | + |
| 53 | + then: |
| 54 | + result.prop == "value" |
| 55 | + } |
| 56 | + |
| 57 | + void "warns when property is set multiple times" () { |
| 58 | + log.isWarnEnabled () >> true |
| 59 | + def builder = new MapBuilder() |
| 60 | + |
| 61 | + when: |
| 62 | + builder.with { |
| 63 | + prop "first-value" |
| 64 | + prop "second-value" |
| 65 | + } |
| 66 | + |
| 67 | + builder.get() |
| 68 | + |
| 69 | + then: |
| 70 | + 1 * log.warn (*_) >> { |
| 71 | + assert it[1][0] == "prop" |
| 72 | + assert it[1][1] == "first-value" |
| 73 | + assert it[1][2] == "second-value" |
| 74 | + true |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + void "does create nested maps for nested dsl closures"() { |
| 79 | + def builder = new MapBuilder() |
| 80 | + |
| 81 | + when: |
| 82 | + builder.with { |
| 83 | + nested1 { |
| 84 | + nested2 { |
| 85 | + nested3 { |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + def result = builder.get() |
| 92 | + |
| 93 | + then: |
| 94 | + result.nested1 instanceof Map |
| 95 | + result.nested1.nested2 instanceof Map |
| 96 | + result.nested1.nested2.nested3 instanceof Map |
| 97 | + } |
| 98 | + |
| 99 | + void "sets nested properties"() { |
| 100 | + def builder = new MapBuilder() |
| 101 | + |
| 102 | + when: |
| 103 | + builder.with { |
| 104 | + nested1 { |
| 105 | + prop1 "value1" |
| 106 | + |
| 107 | + nested2 { |
| 108 | + prop2 "value2" |
| 109 | + |
| 110 | + nested3 { |
| 111 | + prop3 "value3" |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + def result = builder.get() |
| 118 | + |
| 119 | + then: |
| 120 | + result.nested1.prop1 == "value1" |
| 121 | + result.nested1.nested2.prop2 == "value2" |
| 122 | + result.nested1.nested2.nested3.prop3 == "value3" |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments