spec/rubabel/molecule_spec.rb in rubabel-0.2.0 vs spec/rubabel/molecule_spec.rb in rubabel-0.2.1

- old
+ new

@@ -6,10 +6,21 @@ describe 'creation' do it 'can be made with Rubabel[]' do mol = Rubabel["CC(O)O"] mol.csmiles.should == "CC(O)O" end + + it 'can be made with an existing obmol object' do + mol = Rubabel["CC(O)O"] + mol_dup = Rubabel::Molecule.new(mol.ob) + end + + it 'can be made fresh' do + mol = Rubabel::Molecule.new + mol.atoms.size == 0 + mol.title.should == '' + end end #xit 'can add a hydrogen to the formula' do #mol = Rubabel["CCC"] #p mol.formula @@ -56,40 +67,74 @@ specify '== means the canonical smiles strings (:csmiles) are equal' do mol1 = Rubabel["CCO"] mol2 = Rubabel["OCC"] (mol1 == mol2).should be_true - mol2.atoms[0].charge += 1 + mol2[0].charge += 1 (mol1 == mol2).should be_false mol3 = Rubabel["CCCO"] (mol1 == mol3).should be_false end - specify '#add_atom! adds an atom given an atomic number and returns it' do - mol = Rubabel["CCO"] - before_size = mol.atoms.size - atom = mol.add_atom!(6) - atom.el.should == :c - (mol.atoms.size - before_size).should == 1 - mol.csmiles.should == "CCO.C" + 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 + ar = mol[1..-1] + ar.first.el.should == :c + ar.last.el.should == :o + ar.size.should == 2 end + describe 'adding an atom' do + it 'can be added but not attached' do + mol = Rubabel["CCO"] + atom = mol.add_atom!(:n) + atom.el.should == :n + atom = mol.add_atom!(8) + atom.el.should == :o + mol.csmiles.should == "CCO.N.O" + end + + it "can be added and attached by el symbol or atomic number" do + mol = Rubabel["CCO"] + first_carbon = mol[0] + mol.add_atom!(:n, first_carbon) + mol.csmiles.should == "NCCO" + + mol.add_atom!(16, first_carbon) + mol.csmiles.should == "NC(CO)S" + end + + end + + specify '#<< adds atom to the last atom and returns the mol' do + mol = Rubabel::Molecule.new + # by element symbol or atomic number + reply = (mol << :n << :c) + reply.should be_a(Rubabel::Molecule) + reply.csmiles.should == 'CN' + end + specify '#dup duplicates the molecule' do mol = Rubabel["CCO"] dup_mol = mol.dup - mol.atoms[0].ob.set_atomic_num(9) + mol[0].ob.set_atomic_num(9) mol.csmiles.should == "OCF" dup_mol.csmiles.should == "CCO" end 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) - mol.add_bond!(mol.atoms[1], atom) + mol.add_bond!(mol[1], atom) mol.csmiles.should == '*C(O)C' end end specify '#atom(id) retrieves atom by id num' do @@ -146,10 +191,10 @@ describe 'making carbo-cations: spin_multiplicity and charges' do # http://openbabel.org/docs/2.3.1/Features/Radicals.html subject { mol = Rubabel["CC"] } it 'can be turned into a carbocation' do mol = subject - c = mol.atoms[0] + c = mol[0] c.ob.set_spin_multiplicity 2 c.charge += 1 mol.csmiles.should == "C[CH2+]" end end