Module:CineMol/api/testcases

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
-- This is a port of CineMol to lua
-- CineMol https://github.com/moltools/CineMol was written by David Meijer, Marnix H. Medema & Justin J. J. van der Hooft and is MIT licensed
-- Please consider any edits made to this page as dual licensed MIT & CC-BY-SA 4.0

-- Unit tests for [[Module:CineMol/api]]. Click talk page to run tests.

require('strict')
local p = require('Module:UnitTests')

local api = require( 'Module:CineMol/api' )
local Atom = api.Atom 
local Bond = api.Bond 
local Look = api.Look 
local Style = api.Style 
local draw_atoms_in_spacefilling_style = api.draw_atoms_in_spacefilling_style 
local draw_bonds_in_tube_style = api.draw_bonds_in_tube_style 
local draw_bonds_in_wireframe_style = api.draw_bonds_in_wireframe_style 
local draw_molecule = api.draw_molecule 
local filter_atoms_and_bonds = api.filter_atoms_and_bonds 
local CylinderCapType = require( 'Module:CineMol/geometry' ).CylinderCapType
local Scene = require( 'Module:CineMol/model' ).Scene
local Color = require( 'Module:CineMol/style' ).Color

function p:test_filter_atoms_and_bonds()
        -- """Test the filter_atoms_and_bonds function without filtering."""
        local atoms = {
            Atom(0, "C", {0, 0, 0}),
            Atom(1, "H", {1, 0, 0}),
            Atom(2, "H", {0, 1, 0}),
            Atom(3, "H", {0, 0, 1})
        }
        local bonds = {
            Bond(0, 1, 1),
            Bond(0, 2, 1),
            Bond(0, 3, 1),
        }
        local filtered_atoms, filtered_bonds = filter_atoms_and_bonds(atoms, bonds)
        self:equals("filtered atoms", #filtered_atoms, 4)
        self:equals("filtered bonds", #filtered_bonds, 3)
end


function p:test_filter_atoms_and_bonds_with_filtering()
        local atoms = {
            Atom(0, "C", {0, 0, 0}),
            Atom(1, "H", {1, 0, 0}),
            Atom(2, "H", {0, 1, 0}),
            Atom(3, "H", {0, 0, 1})
        }
        local bonds = {
            Bond(0, 1, 1),
            Bond(0, 2, 1),
            Bond(0, 3, 1),
        }
        local filtered_atoms, filtered_bonds = filter_atoms_and_bonds(atoms, bonds, {"H"})
        self:equals("filtered atoms", #filtered_atoms, 1)
        self:equals("filtered bonds", #filtered_bonds, 0)
end

function p:test_filter_atoms_and_bonds_with_filtering_out_all_atoms()
        local atoms = {
            Atom(0, "C", {0, 0, 0}),
            Atom(1, "H", {1, 0, 0}),
            Atom(2, "H", {0, 1, 0}),
            Atom(3, "H", {0, 0, 1})
        }
        local bonds = {
            Bond(0, 1, 1),
            Bond(0, 2, 1),
            Bond(0, 3, 1),
        }
        local filtered_atoms, filtered_bonds = filter_atoms_and_bonds(atoms, bonds, {"C", "H"})
        self:equals("filtered atoms", #filtered_atoms, 0)
        self:equals("filtered bonds", #filtered_bonds, 0)
end

function p:test_draw_bonds_in_wireframe_style_without_supplying_bonds()
        local scene = Scene()
        local atoms = {
            Atom(0, "C", {0, 0, 0}),
            Atom(1, "H", {1, 0, 0}),
            Atom(2, "H", {0, 1, 0}),
            Atom(3, "H", {0, 0, 1})
        }
        draw_bonds_in_wireframe_style(scene, atoms, {})
        self:equals("nodes", #scene.nodes, 0)
end

function p:test_draw_bonds_in_wireframe_style()
		local scene = Scene()
        local atoms = {
            Atom(0, "C", {0, 0, 0}),
            Atom(1, "H", {1, 0, 0}),
            Atom(2, "H", {0, 1, 0}),
            Atom(3, "H", {0, 0, 1})
        }
        local bonds = {
            Bond(0, 1, 1),
            Bond(0, 2, 1),
            Bond(0, 3, 1),
        }
        draw_bonds_in_wireframe_style(scene, atoms, bonds)
        self:equals('nodes', #scene.nodes, 6)  -- 3 x 2 half bonds.
end

function p:test_draw_atoms_in_spacefilling_style()

        local scene = Scene()
        local atoms = {
            Atom(0, "C", {0, 0, 0}),
            Atom(1, "H", {1, 0, 0}),
            Atom(2, "H", {0, 1, 0}),
            Atom(3, "H", {0, 0, 1})
        }
        draw_atoms_in_spacefilling_style(scene, atoms, Look.CARTOON, Color(255, 255, 255), 0.05)
        self:equals("nodes", #scene.nodes, 4)
end

function p:test_draw_atoms_in_tube_style()
        local scene = Scene()
        local atoms = {
            Atom(0, "C", {0, 0, 0}),
            Atom(1, "H", {1, 0, 0}),
            Atom(2, "H", {0, 1, 0}),
            Atom(3, "H", {0, 0, 1})
        }

        draw_bonds_in_tube_style(
            scene,
            atoms,
            {},
            Style.TUBE,
            Look.CARTOON,
            CylinderCapType.NO_CAP,
            Color(255, 255, 255),
            0.05
        )
        self:equals("nodes", #scene.nodes, 0)
end

function p:test_draw_molecule_in_spacefilling_style()
        local svg = draw_molecule(
        	{
            	Atom(0, "C", {0, 0, 0}),
            	Atom(1, "H", {1, 0, 0}),
            	Atom(2, "H", {0, 1, 0}),
            	Atom(3, "H", {0, 0, 1})
        	},
        	{
            	Bond(0, 1, 1),
            	Bond(0, 2, 1),
            	Bond(0, 3, 1),
        	},
            Style.SPACEFILLING,
            Look.CARTOON,
            50
        )
        self:equals("objects", #svg.objects, 4)
        self:equals("fills", #svg.fills, 4)
end

function p:test_draw_molecule_in_spacefilling_style_glossy()
        local svg = draw_molecule(
        	{
            	Atom(0, "C", {0, 0, 0}),
            	Atom(1, "H", {1, 0, 0}),
            	Atom(2, "H", {0, 1, 0}),
            	Atom(3, "H", {0, 0, 1})
        	},
        	{
            	Bond(0, 1, 1),
            	Bond(0, 2, 1),
            	Bond(0, 3, 1),
        	},
            Style.SPACEFILLING,
            Look.GLOSSY,
            50
        )
        self:equals("objects", #svg.objects, 4)
        self:equals("fills", #svg.fills, 4)
end


function p:test_draw_molecule_in_tube_style()
        local svg = draw_molecule(
        	{
            	Atom(0, "C", {0, 0, 0}),
            	Atom(1, "H", {1, 0, 0}),
            	Atom(2, "H", {0, 1, 0}),
            	Atom(3, "H", {0, 0, 1})
        	},
        	{
            	Bond(0, 1, 1),
            	Bond(0, 2, 1),
            	Bond(0, 3, 1),
        	},
            Style.TUBE,
            Look.CARTOON,
            50
        )
        self:equals("objects", #svg.objects, 6)
        self:equals("fills", #svg.fills, 6)
end


function p:test_draw_molecule_in_wireframe_style()
        local svg = draw_molecule(
        	{
            	Atom(0, "C", {0, 0, 0}),
            	Atom(1, "H", {1, 0, 0}),
            	Atom(2, "H", {0, 1, 0}),
            	Atom(3, "H", {0, 0, 1})
        	},
        	{
            	Bond(0, 1, 1),
            	Bond(0, 2, 1),
            	Bond(0, 3, 1),
        	},
            Style.WIREFRAME,
            Look.CARTOON,
            50
        )
        self:equals("objects", #svg.objects, 6)
        self:equals("fills", #svg.fills, 6)
end


function p:test_draw_molecule_with_single_colored_triple_bond_in_ball_and_stick_style()
        local svg = draw_molecule(
        	{
            	Atom(0, "C", {0, 0, 0}),
            	Atom(1, "C", {1, 0, 0}),
        	},
        	{
            	Bond(0, 1, 3)
        	},
            Style.BALL_AND_STICK,
            Look.CARTOON,
            50
        )
        self:equals("objects", #svg.objects, 5)
        self:equals("fills", #svg.fills, 5)
end


function p:test_draw_molecule_with_multi_colored_triple_bond_in_ball_and_stick_style()
        local svg = draw_molecule(
        	{
            	Atom(0, "C", {0, 0, 0}),
            	Atom(1, "N", {1, 0, 0}),
        	},
        	{
            	Bond(0, 1, 3)
        	},
            Style.BALL_AND_STICK,
            Look.CARTOON,
            50
        )
        self:equals("objects", #svg.objects, 8)
        self:equals("fills", #svg.fills, 8)
end


function p:test_draw_molecule_with_single_and_triple_bonds_in_ball_and_stick_style()
        local svg = draw_molecule(
        	{
            	Atom(0, "C", {0, 0, 0}),
            	Atom(1, "H", {1, 0, 0}),
            	Atom(2, "N", {0, 1, 0}),
        	},
        	{
            	Bond(0, 1, 1),
            	Bond(0, 2, 3),
        	},
            Style.BALL_AND_STICK,
            Look.CARTOON,
            50
        )
        self:equals("objects", #svg.objects, 11)
        self:equals("fills", #svg.fills, 11)
end

return p