Last active
December 16, 2015 05:09
-
-
Save yasuoza/5382093 to your computer and use it in GitHub Desktop.
TDDBC Tokyo 2013-03 のお題にチャレンジしてみた (お題の参考)https://github.com/sue445/tddbc_tokyo_20130316
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package LTSV; | |
use strict; | |
use warnings; | |
use feature qw/ say /; | |
sub new { | |
my $pkg = shift; | |
bless{ | |
attributes => {}, | |
}, $pkg; | |
} | |
sub set { | |
my ($pkg, $key, $val) = @_; | |
$pkg->{attributes}->{$key} = $val; | |
} | |
sub get { | |
my ($pkg, $key) = @_; | |
$pkg->{attributes}->{$key}; | |
} | |
sub dump { | |
my $pkg = shift; | |
my $str = ''; | |
while ( my ($key, $val) = each($pkg->{attributes}) ) { | |
$str .= $key . ':' . $val . "\t"; | |
} | |
$str =~ s/\t$//; | |
$str . "\n"; | |
} | |
sub parse { | |
my ($class, $str) = @_; | |
my $ltsv = $class->new; | |
chomp($str); | |
my @kvs = split("\t", $str); | |
foreach my $kv (@kvs) { | |
my ($key, $val) = split(':', $kv, 2); | |
$ltsv->set($key, $val); | |
} | |
$ltsv; | |
} | |
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LTSV | |
def initialize | |
@attributes = {} | |
end | |
def set(key, val) | |
raise ArgumentError if key.nil? || key.empty? | |
@attributes[key] = val | |
self | |
end | |
def get(key) | |
@attributes[key] | |
end | |
def dump | |
@attributes.map{|key, val| "#{key}:#{val}"}.join("\t").tap { |str| | |
str.concat("\n") unless str.empty? | |
} | |
end | |
class << self | |
def parse(row) | |
LTSV.new.tap do |ltsv| | |
row.chomp.split("\t").map { |kv| | |
key, val = kv.split(':', 2) | |
ltsv.set(key, val) | |
} | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Test::More; | |
BEGIN { use_ok('LTSV'); } | |
my $ltsv = LTSV->new; | |
ok($ltsv->set('foo', 'bar'), 'LTSV provides set method'); | |
is($ltsv->get('foo'), 'bar', 'LTSV provides get method'); | |
$ltsv->set('foo', 'hoge'); | |
is($ltsv->get('foo'), 'hoge', 'LTSV#set overrides key'); | |
$ltsv->set('hoge', 'fuga'); | |
is($ltsv->dump, "foo:hoge\thoge:fuga\n", 'LTSV provides dump method'); | |
{ | |
my $ltsv = LTSV->parse("foo:hoge\thoge:fuga\n"); | |
is($ltsv->get('foo'), 'hoge', 'LTSV parses given string'); | |
is($ltsv->get('hoge'), 'fuga', 'LTSV parses given string'); | |
} | |
done_testing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test/unit' | |
require File.expand_path('../ltsv', __FILE__) | |
class TestLTSV < Test::Unit::TestCase | |
def setup | |
@ltsv = LTSV.new | |
end | |
def test_set | |
assert_nothing_raised ArgumentError do | |
@ltsv.set('foo', 'hoge') | |
end | |
end | |
def test_get | |
@ltsv.set('foo', 'hoge') | |
assert_equal 'hoge', @ltsv.get('foo') | |
end | |
def test_not_key_get | |
@ltsv.set('foo', 'hoge') | |
assert_nil @ltsv.get('bar') | |
end | |
def test_dump | |
@ltsv.set('hoge', 'fuga').set('foo', 'bar') | |
assert_equal "hoge:fuga\tfoo:bar\n", @ltsv.dump | |
end | |
def test_override_value | |
@ltsv.set('foo', 'bar').set('foo', 'piyo') | |
assert_equal 'piyo', @ltsv.get('foo') | |
end | |
def test_parse | |
ltsv = LTSV.parse("foo:hoge\tbar:fuga\n") | |
assert_equal 'hoge', ltsv.get('foo') | |
assert_equal 'fuga', ltsv.get('bar') | |
end | |
def test_raise_nil_key | |
assert_raise ArgumentError do | |
@ltsv.set(nil, 'hey') | |
end | |
end | |
def test_raise_empty_key | |
assert_raise ArgumentError do | |
@ltsv.set('', 'hey') | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment