require 'spec' require File.dirname(__FILE__) + '/../lib/hyogen' context "deviding csvlines into tables:" do specify "empty" do csvdata = CSV.parse("") table_list = Hyogen::Reader.devide_into_table(Hyogen::Reader::Matrix.new(csvdata)) table_list.should_eql [] end specify "the smallest table" do csvdata = CSV.parse("{VTABLE}") table_list = Hyogen::Reader.devide_into_table(Hyogen::Reader::Matrix.new(csvdata)) table_list.size.should_eql 1 table_list.first.to_a.should_eql [["{VTABLE}"]] end specify "the single table" do csvdata = CSV.parse("{HTABLE}\na,b\n1,2") table_list = Hyogen::Reader.devide_into_table(Hyogen::Reader::Matrix.new(csvdata)) table_list.size.should_eql 1 table_list.first.to_a.should_eql [["{HTABLE}"],["a","b"],["1","2"]] end specify "the multiple table" do csvdata = CSV.parse("{VTABLE}\n{L}\na,b\n{HTABLE}\n{L}\n1,2") table_list = Hyogen::Reader.devide_into_table(Hyogen::Reader::Matrix.new(csvdata)) table_list.size.should_eql 2 table_list[0].to_a.should_eql [["{VTABLE}"],["{L}"],["a","b"]] table_list[1].to_a.should_eql [["{HTABLE}"],["{L}"],["1","2"]] end end context "parse tables:" do specify "empty" do csvdata = CSV.parse("") tables = Hyogen.parse_tables(csvdata) tables.should_eql [] end specify "single table" do csvdata = CSV.parse("{VTABLE}\n") tables = Hyogen.parse_tables(csvdata) tables.size.should_equal 1 tables.first.title.should == "" end specify "labeled table" do csvdata = CSV.parse("test table{VTABLE}\n") tables = Hyogen.parse_tables(csvdata) tables.size.should == 1 tables.first.title.should_eql "test table" end specify "multiple tables" do csvdata = CSV.parse("{VTABLE}\n{L}\n{HTABLE}\n{L}") tables = Hyogen.parse_tables(csvdata) tables.size.should_equal 2 end end context "parse label:" do expect = [{"l1"=>"A"}, {"l2"=>"B"}] specify "hlabel" do csvdata = CSV.parse("test data{VTABLE}\n{L}l1,l2\n,A\n,B") tables = Hyogen.parse_tables(csvdata) tables.size.should_equal 1 table = tables.first table.labels.size.should == 2 table.labels[0].names.should == ["l1"] table.labels[1].names.should == ["l2"] end specify "vlabel" do csvdata = CSV.parse("test data{HTABLE}\n{L}l1,A\nl2,B") tables = Hyogen.parse_tables(csvdata) tables.size.should_equal 1 table = tables.first table.labels.size.should == 2 table.labels[0].names.should == ["l1"] table.labels[1].names.should == ["l2"] end end #単純な(ラベル数=1)表 context "Table has single label:" do expect = [{"l"=>"A"}, {"l"=>"B"}] specify "vtable label" do csvdata = CSV.parse("test data{VTABLE}\n{L}l\nA\nB") tables = Hyogen.parse_tables(csvdata) tables.size.should_equal 1 tables.first.records.should == expect end specify "htable label" do csvdata = CSV.parse("test data{HTABLE},,\nl{L},A,B") tables = Hyogen.parse_tables(csvdata) tables.size.should_equal 1 table = tables.first table.records.should == expect end end #冗長なデータを含む表 context "redudancy:" do specify "table" do csvdata = CSV.parse("{VTABLE}\n\n{L}a,b\n\n{HTABLE}\n{L}1,2\n\nhoge") table_list = Hyogen::Reader.devide_into_table(Hyogen::Reader::Matrix.new(csvdata)) table_list.size.should_eql 2 table_list[0].to_a.should_eql [["{VTABLE}"],[nil],["{L}a","b"]] table_list[1].to_a.should_eql [["{HTABLE}"],["{L}1","2"]] end specify "data" do expect = [{"l"=>"A"}, {"l"=>"B"}] csvdata = CSV.parse("test data {VTABLE} \n\n {L} l \n A \nB \n") tables = Hyogen.parse_tables(csvdata) tables.size.should_equal 1 tables.first.title.should == "test data" tables.first.records.should == expect end end #複数のレコードと複数のラベルを持つ表 context "Table has multiple records and multiple labels:" do expect = [{"name"=>"A", "age"=>"10"}, {"name"=>"B", "age"=>"20"}] specify "vertical" do csvdata = CSV.parse("{VTABLE}\n{L}name,age\nA,10\nB,20") table = Hyogen.parse_tables(csvdata).first table.records.should == expect end specify "horizontal" do csvdata = CSV.parse("{HTABLE},,\n{L}name,A,B\nage,10,20") table = Hyogen.parse_tables(csvdata).first table.records.should == expect end end #階層のある表 context "Table is structured" do expect = [{"category"=> {"type" => "C"}}] specify "vtable" do csvdata = CSV.parse("{VTABLE}\n{L}category\n{L}type\nC") table = Hyogen.parse_tables(csvdata).first table.records.should == expect end specify "htable" do csvdata = CSV.parse("{HTABLE},,o\n{L}category,{L}type,C") table = Hyogen.parse_tables(csvdata).first table.records.should == expect end end #補足タグ context "Note Tag" do expect_records = [ {""=>"id1", "name"=> "alice", "age" => "10"}, {""=>"id2", "name"=> "bob", "age" => "20"}, ] specify "" do csvdata = CSV.parse("{HTABLE}\n{L},id1,id2,{N}補足\nname,alice,bob,名前\nage,10,20,歳") table = Hyogen.parse_tables(csvdata).first table.records.should == expect_records table.labels[0].note.should == {"補足" => "補足"} table.labels[1].note.should == {"補足" => "名前"} table.labels[2].note.should == {"補足" => "歳"} end end context "Matrix of Excel Worksheet:" do specify "" do file = "./spec/sample1.xls" workbook = Spreadsheet::ParseExcel.parse(file) ws = workbook.worksheet(0) matrix = Hyogen::Reader::worksheet_to_matrix(ws) matrix.row_size.should == 4 matrix.column_size(0).should == 1 matrix.column_size(1).should == 4 matrix.value(0,0).should == "{HTABLE}" matrix.row(1).should == ["{L}", "id1", "id2", "{N}補足"] matrix.to_a.should == [ ["{HTABLE}"], ["{L}", "id1", "id2", "{N}補足"], ["name", "Alice", "Bob", "名前"], ["age", "10.0", "20.0", "歳"] ] end end #各種ファイルフォーマット context "File formats" do def check table expect_records = [ {""=>"id1", "name"=> "Alice", "age" => "10.0"}, {""=>"id2", "name"=> "Bob", "age" => "20.0"}, ] table.records.should == expect_records table.labels[0].note.should == {"補足" => "補足"} table.labels[1].note.should == {"補足" => "名前"} table.labels[2].note.should == {"補足" => "歳"} end specify "csv string" do input = "{HTABLE}\n{L},id1,id2,{N}補足\nname,Alice,Bob,名前\nage,10.0,20.0,歳" check Hyogen.parse_tables(input).first end specify "csv array" do input = CSV.parse("{HTABLE}\n{L},id1,id2,{N}補足\nname,Alice,Bob,名前\nage,10.0,20.0,歳") check Hyogen.parse_tables(input).first end specify "csv file" do input = "./spec/sample1.csv" check Hyogen.parse_tables(input).first end specify "xls file" do input = "./spec/sample1.xls" check Hyogen.parse_tables(input).first end end #省略 context "label syoryaku" do specify "vtable" do csvdata = CSV.parse("{VTABLE},\n{L}a,\n{L}t1,t2\n1,2") expect = [{"a"=> {"t1" => "1", "t2" => "2"}}] table = Hyogen.parse_tables(csvdata).first table.records.should == expect end specify "overwrite" do csvdata = CSV.parse("{VTABLE},\n{L}a,\n{L}t,\n1,2") expect = [{"a"=> {"t" => "2"}}] table = Hyogen.parse_tables(csvdata).first table.records.should == expect end end #置換 context "alias" do specify "label" do csvdata = CSV.parse("{VTABLE},\n{L}a,\n{L}t1,t2\n1,2") expect = [{"a"=> {"t1" => "1", "abc" => "2"}}] table = Hyogen.parse_tables(csvdata).first table.records.should == expect end specify "value" do csvdata = CSV.parse("{VTABLE},\n{L}a,\n{L}t1,t2\n1,2 <2.00>") expect = [{"a"=> {"t1" => "1", "t2" => "2.00"}}] table = Hyogen.parse_tables(csvdata).first table.records.should == expect end end #生成 context "generate:" do def check table expect_records = [ {""=>"id1", "name"=> "alice", "age" => "10.0"}, {""=>"id2", "name"=> "bob", "age" => "20.0"}, ] table.records.should == expect_records table.labels[0].note.should == {"補足" => "補足"} table.labels[1].note.should == {"補足" => "名前"} table.labels[2].note.should == {"補足" => "歳"} end specify "from script" do input = "{HTABLE}\n{L},id1,id2,{N}補足\nname,Alice,Bob,名前\nage,10.0,20.0,歳" tables = Hyogen.parse_tables(input) begin File.delete('./spec/sample1.txt') rescue Errno::ENOENT end Hyogen::Template.generate(tables, './spec/sample1.txt.template', './spec') result = File.open('./spec/sample1.txt') {|f| f.read} result.should == "My name is Alice. I'm 10 years old.\nMy name is Bob. I'm 20 years old.\n" end specify "from shell" do begin File.delete('./spec/sample1.txt') rescue Errno::ENOENT end result = system 'ruby', 'bin/hyogen', '-s', './spec/sample1.xls', './spec/sample1.txt.template', '-o', './spec' result.should == true result = File.open('./spec/sample1.txt') {|f| f.read} result.should == "My name is Alice. I'm 10 years old.\nMy name is Bob. I'm 20 years old.\n" end end