mirror of
https://github.com/zap-proto/ruby.git
synced 2026-10-30 15:04:01 +00:00
Rebrand all ZAP-facing identifiers off Cap'n Proto: - module Capnp→Zap, ext binding namespace ruby_capn_proto→ruby_zap, header guard RUBY_CAPN_PROTO_H→RUBY_ZAP_H, entry point Init_zap, gem capn_proto→zap, schemas .capnp→.zap, media/logo, gemspec, README, spec, examples. Zero capnp/Cap'n Proto brand prose remains. Binding-namespace nuance (kept on purpose): the native ext links the upstream serialization runtime (libcapnp/kj). Its capnp:: C++ symbols and <capnp/...> headers are the linked library's real API — there is no zap:: namespace or libzap to retarget to yet — so those references, the -lcapnp/-lcapnpc/-lkj link flags, and the pycapnp dev-only fixture generators stay, each documented as the external dependency surface (retargets to <zap/...>/-lzap when the ZAP C++ runtime ships). Build/modernization: - extconf.rb: discover the runtime via dir_config + pkg_config so the ext builds against non-default prefixes (e.g. Homebrew), not just /usr/local. Native ext now compiles + links cleanly (zap.bundle). - ext: parenthesize `if (FIXNUM_P(x))` in the two dynamic builders — required since Ruby's FIXNUM_P macro no longer wraps its expansion (Ruby 4 build break). - Rakefile: put lib on $LOAD_PATH and load the gemspec via Gem::Specification.load; pin rake-compiler ~> 0.9; make the rspec rake task degrade with a clear message instead of aborting the whole Rakefile when the harness can't load. - gemspec: bump dev deps within-major (rspec ~> 2.14, rake ~> 13.0, rake-compiler ~> 0.9); add required_ruby_version. - .gitignore: ignore native-ext build artifacts (*.o, Makefile, mkmf.log, *.bundle.dSYM, *.gem). Verified: all spec/zap_spec.rb assertions pass against the compiled binding (write/read + to_bytes round-trips, unknown-field and out-of-bounds raise Zap::Exception, dynamic struct assign); the addressbook example decodes end-to-end.
81 lines
2.0 KiB
Ruby
81 lines
2.0 KiB
Ruby
require 'tempfile'
|
|
require 'spec_helper'
|
|
require 'zap'
|
|
|
|
ADDRESSBOOK_SCHEMA = File.expand_path("../addressbook.zap", __FILE__)
|
|
|
|
module AddressBook extend Zap::SchemaLoader
|
|
load_schema(ADDRESSBOOK_SCHEMA)
|
|
end
|
|
|
|
describe "reading" do
|
|
describe "Dynamic Structs" do
|
|
let(:file) { open_file("addressbook.bin") }
|
|
let(:addresses) { AddressBook::AddressBook.read_from(file) }
|
|
|
|
it "unkown fields raise exception" do
|
|
expect { addresses.foo }.to raise_error(Zap::Exception)
|
|
end
|
|
end
|
|
|
|
describe "Dynamic Lists" do
|
|
let(:file) { open_file("addressbook.bin") }
|
|
let(:addresses) { AddressBook::AddressBook.read_from(file) }
|
|
let(:people) { addresses.people }
|
|
|
|
it "#[] given out-of-bounds index raises" do
|
|
expect { addresses.people[999999] }.to raise_error(Zap::Exception)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe "writing" do
|
|
describe "#write" do
|
|
it "full circle" do
|
|
tmp = Tempfile.new('test.bin')
|
|
addresses = AddressBook::AddressBook.new_message
|
|
people = addresses.initPeople(1)
|
|
bob = people[0]
|
|
bob.name = "Bob"
|
|
bob.id = 123
|
|
addresses.write(tmp)
|
|
|
|
tmp.rewind
|
|
addresses = AddressBook::AddressBook.read_from(tmp)
|
|
expect(addresses.people.size).to eq 1
|
|
expect(addresses.people.first.name).to eq "Bob"
|
|
expect(addresses.people.first.id).to eq 123
|
|
end
|
|
end
|
|
|
|
describe "#to_bytes" do
|
|
it "full circle" do
|
|
addresses = AddressBook::AddressBook.new_message
|
|
people = addresses.initPeople(1)
|
|
bob = people[0]
|
|
bob.name = "Bob"
|
|
bob.id = 123
|
|
bytes = addresses.to_bytes
|
|
|
|
addresses = AddressBook::AddressBook.make_from_bytes(bytes)
|
|
expect(addresses.people.size).to eq 1
|
|
expect(addresses.people.first.name).to eq "Bob"
|
|
expect(addresses.people.first.id).to eq 123
|
|
end
|
|
end
|
|
|
|
describe "Dynamic Structs" do
|
|
let(:addresses) { AddressBook::AddressBook.new_message }
|
|
|
|
it "works" do
|
|
people = addresses.initPeople(3)
|
|
bob = people.first
|
|
bob.name = "Bob"
|
|
bob.id = 10
|
|
|
|
people[1] = bob
|
|
x = people[1]
|
|
end
|
|
end
|
|
end
|