Add specs for environment class

This commit is contained in:
Andrew Tomaka 2018-04-09 17:54:30 -04:00
parent c7e33266ec
commit 2891c0d168
Signed by: atomaka
GPG key ID: 61209BF70A5B18BE
2 changed files with 65 additions and 2 deletions

View file

@ -1,11 +1,11 @@
module Psenv
class Environment < Hash
def apply
each { |k, v| ENV.store(k, v) unless ENV.has_key?(k) }
each { |k, v| ENV.store(k.to_s, v) unless ENV.has_key?(k.to_s) }
end
def apply!
each { |k, v| ENV.store(k, v) }
each { |k, v| ENV.store(k.to_s, v) }
end
def self.create(*variables)

63
spec/environment_spec.rb Normal file
View file

@ -0,0 +1,63 @@
require "spec_helper"
RSpec.describe Psenv::Environment do
let(:environment1) { { A: "1", B: "1" } }
let(:environment2) { { B: "2", C: "2" } }
context ".create" do
subject { Psenv::Environment.create(environment1, environment2) }
it "returns an environment object" do
expect(subject).to be_kind_of(Psenv::Environment)
end
it "is also a hash" do
expect(subject).to be_kind_of(Hash)
end
it "creates the correct environment" do
expected = { A: "1", B: "1", C: "2" }
expect(subject).to eq(expected)
end
end
context "#apply" do
before(:each) do
ENV.store("A", "0")
environment = Psenv::Environment.create(environment1, environment2)
environment.apply
end
it "does not overwrite existing environment" do
expect(ENV["A"]).to eq("0")
end
it "applies unique variables as expected" do
expect(ENV["C"]).to eq("2")
end
it "applies duplicates based on the first provided" do
expect(ENV["B"]).to eq("1")
end
end
context "#apply!" do
before(:each) do
ENV.store("A", "0")
environment = Psenv::Environment.create(environment1, environment2)
environment.apply!
end
it "does overwrite existing environment" do
expect(ENV["A"]).to eq("1")
end
it "applies unique variables as expected" do
expect(ENV["C"]).to eq("2")
end
it "applies duplicates based on the first provided" do
expect(ENV["B"]).to eq("1")
end
end
end