spec/rubabel/molecule_spec.rb in rubabel-0.2.2 vs spec/rubabel/molecule_spec.rb in rubabel-0.3.0

- old
+ new

@@ -20,14 +20,14 @@ mol.title.should == '' end end #xit 'can add a hydrogen to the formula' do - #mol = Rubabel["CCC"] - #p mol.formula - #mol.add_hydrogen_to_formula! - #p mol.formula + #mol = Rubabel["CCC"] + #p mol.formula + #mol.add_hydrogen_to_formula! + #p mol.formula #end describe 'png output' do it 'creates a png image (corresponds to the svg)' do mol = Rubabel["NCC(=O)O"] @@ -75,18 +75,18 @@ (mol1 == mol3).should be_false end specify '#[] retrieves atom by index' do mol = Rubabel["NCO"] - mol[0].el.should == :n - mol[1].el.should == :c - mol[2].el.should == :o - mol[-1].el.should == :o - mol[-2].el.should == :c + mol[0].el.should == :N + mol[1].el.should == :C + mol[2].el.should == :O + mol[-1].el.should == :O + mol[-2].el.should == :C ar = mol[1..-1] - ar.first.el.should == :c - ar.last.el.should == :o + ar.first.el.should == :C + ar.last.el.should == :O ar.size.should == 2 end specify 'num_atoms gives the number of atoms which can vary with hydrogens added' do mol = Rubabel["CCC"] @@ -98,38 +98,48 @@ specify 'num_atoms(true) gives the number including implied hydrogens' do mol = Rubabel["CCC"] mol.num_atoms(true).should == 11 end - describe 'adding an atom' do - it 'can be added but not attached' do + describe 'adding or associating atoms' do + + it 'can associate atoms with the molecule' do mol = Rubabel["CCO"] - atom = mol.add_atom!(:n) - atom.el.should == :n - atom = mol.add_atom!(8) - atom.el.should == :o + nitrogen = mol.associate_atom!(:N) + nitrogen.el.should == :N + oxygen = mol.associate_atom!(8) + oxygen.el.should == :O mol.csmiles.should == "CCO.N.O" + + oxygen_aromatic = mol.associate_atom!(:O) + mol.csmiles.should == "CCO.N.O.O" end - it "can be added and attached by el symbol or atomic number" do + it "can be added and attached by element symbol or atomic number" do mol = Rubabel["CCO"] first_carbon = mol[0] - mol.add_atom!(:n, first_carbon) + mol.add_atom!(:N, 1, first_carbon) mol.csmiles.should == "NCCO" - mol.add_atom!(16, first_carbon) + mol.add_atom!(16, 1, first_carbon) mol.csmiles.should == "NC(CO)S" end end - - specify '#<< adds atom to the last atom and returns the mol' do + + specify '#<< adds atom to the last atom and returns the added atom' do mol = Rubabel::Molecule.new + reply = (mol << :N) # by element symbol or atomic number - reply = (mol << :n << :c) - reply.should be_a(Rubabel::Molecule) - reply.csmiles.should == 'CN' + reply = (mol << :N << :C) + reply.should be_a(Rubabel::Atom) + reply.el.should == :C + + # properly handles aromaticity + mol.atoms.last.aromatic?.should be_false + mol << :c # aromatic specifier in SMILES + mol.atoms.last.aromatic?.should be_true end specify '#dup duplicates the molecule' do mol = Rubabel["CCO"] dup_mol = mol.dup @@ -141,28 +151,28 @@ specify '#add_atom! with a Rubabel::Atom' describe '#add_bond! adds a bond (and updates atoms)' do specify 'given two atoms' do mol = Rubabel["CCO"] - atom = mol.add_atom!(0) + atom = mol.associate_atom!(0) mol.add_bond!(mol[1], atom) mol.csmiles.should == '*C(O)C' end end specify '#atom(id) retrieves atom by id num' do mol = Rubabel["CCO"] - o = mol.find {|a| a.el == :o } + o = mol.find {|a| a.el == :O } mol.atom(o.id).id.should == o.id end specify '#swap! can swap atoms around' do mol = Rubabel["NCC(=O)O"] carboxy_carbon = mol.atoms.find {|atom| atom.type == 'Cac' } single_bonded_oxygen = mol.atoms.find {|atom| atom.type == 'O.co2' && atom.get_bond(carboxy_carbon).bond_order == 1 } nit_carbon = mol.atoms.find {|atom| atom.atoms.any? {|nbr| nbr.type == 'N3' } } - nitrogen = mol.atoms.find {|atom| atom.el == :n } + nitrogen = mol.atoms.find {|atom| atom.el == :N } swapped = mol.swap!(nit_carbon, nitrogen, carboxy_carbon, single_bonded_oxygen) swapped.should == mol swapped.csmiles.should == 'NC(=O)CO' end @@ -313,10 +323,10 @@ end describe 'breaking a molecule' do before(:each) do @mol = Rubabel::Molecule.from_string("NC(=O)CO") - @n = @mol.find {|a| a.el == :n } + @n = @mol.find {|a| a.el == :N } end it 'num_atoms, atoms and each_atom are sensitive to #add_h!' do @mol.num_atoms.should == 5 @mol.atoms.size.should == 5