Skip to content

Instantly share code, notes, and snippets.

@y-sumida
Last active December 16, 2015 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y-sumida/5381979 to your computer and use it in GitHub Desktop.
Save y-sumida/5381979 to your computer and use it in GitHub Desktop.
package xutp
class KeyValueStore {
private final store = [:]
void put(def key, def value) {
if (key == null) throw new IllegalArgumentException()
store[key] = value
}
void put(def kv) {
kv.each {key, value ->
if (key == null) throw new IllegalArgumentException()
}
kv.each {key, value ->
store[key] = value
}
}
def get(def key) {
if (key == null) throw new IllegalArgumentException()
store[key]
}
void delete(def key) {
store.remove(key)
}
String debugPrint(String key, String value) {
String str
str = "{" + key + ":" + value + "}"
str
}
String dumpAll() {
String result = ""
store.each {key, value ->
result += debugPrint(key, value)
}
result
}
}
package xutp
import spock.lang.Ignore
import spock.lang.Specification
import spock.lang.Unroll
class KeyValueStoreSpec extends Specification {
def kvs
def setup() {
kvs = new KeyValueStore()
}
@Unroll
def "空のKVSにput('#key', '#value')するとget('#key')で’#value'が取得できる"() {
when:
kvs.put(key, value)
then:
kvs.get(key) == value
where:
key |value
'aaa' |'bbb'
1 |2.5
}
def "KVSにnullのkeyをputすると例外が発生する" () {
when:
kvs.put(null, 'null')
then:
thrown(IllegalArgumentException)
}
def "KVSにnullのkeyでgetすると例外が発生する" () {
given:
kvs.put('aaa', 'bbb')
when:
kvs.get(null)
then:
thrown(IllegalArgumentException)
}
def "KVSに登録されていないkeyでgetを行うとnullが返る" () {
when:
kvs.put('aaa', 'bbb')
then:
kvs.get('other') == null
}
def "既にKVSに存在するkeyをputするとvalueのみ更新される" () {
given:
kvs.put('aaa', 'bbb')
when:
kvs.put('aaa', 'ccc')
then:
kvs.get('aaa') == 'ccc'
}
def "既にKVSに存在するkeyについてdeleteを行うとkeyとvalueのペアがKVSから削除される" () {
given:
kvs.put(key, value)
when:
kvs.delete(key)
then:
kvs.get(key) == null
where:
key |value
'aaa' |'bbb'
'ccc' |'ddd'
1 |2.5
}
def "KVSに登録されているkey-valueを文字列で取得できる" () {
when:
kvs.put('aaa', 'bbb')
kvs.put('ccc', 'ddd')
then:
kvs.dumpAll() == "{aaa:bbb}{ccc:ddd}"
}
def "KVSに複数のkeyvalueペアを一度に登録できる" () {
when:
kvs.put(map)
then:
kvs.get(key)
where:
map |key |value
['aaa':'bbb','ccc':'ddd'] | 'aaa' | 'bbb'
['aaa':'bbb','ccc':'ddd'] | 'ccc' | 'ddd'
[1:2.5,10:30.0] | 1 | 2.5
[1:2.5,10:30.0] | 10 | 30.0
}
def "複数keyvalueペアのkeyに一つでもnullが含まれていたらKVSを更新せず例外を発生させる" () {
when:
kvs.put(['aaa':'bbb', (null):'ddd'])
then:
thrown(IllegalArgumentException)
kvs.dumpAll() == ''
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment