#!/usr/bin/perl

use warnings;
use strict;

use Test::More;

use CAD::Mesh3D::FormatSTL;

if ( $^O eq 'freebsd' ) {
    plan skip_all => 'Tests not needed except on Windows, and dont work on FreeBSD';
}

my $stl = CAD::Mesh3D::FormatSTL->new;
isa_ok($stl, 'CAD::Mesh3D::FormatSTL');

my $part = $stl->add_part('cube');
isa_ok($part, 'CAD::Format::STL::part');
is($part->name, 'cube', 'part name');

$part->add_facets(
  [[0,0,0], [0,1,0], [1,1,0]],
  [[0,0,0], [1,1,0], [1,0,0]],
  [[0,0,0], [0,0,1], [0,1,1]],
  [[0,0,0], [0,1,1], [0,1,0]],
  [[0,0,0], [1,0,0], [1,0,1]],
  [[0,0,0], [1,0,1], [0,0,1]],
  [[0,0,1], [1,0,1], [1,1,1]],
  [[0,0,1], [1,1,1], [0,1,1]],
  [[1,0,0], [1,1,0], [1,1,1]],
  [[1,0,0], [1,1,1], [1,0,1]],
  [[0,1,0], [0,1,1], [1,1,1]],
  [[0,1,0], [1,1,1], [1,1,0]],
);
is(scalar($part->facets), 12, 'twelve triangles');

# XXX this is silly, no?
if($ENV{DO_REGEN}) {
  $stl->save("files/cube.stl");
  local $SIG{__DIE__};
  die "       bailing out after regen";
}

{
  my $string;
  open(my $ofh, '>', \$string) or die "ack";
  $stl->save($ofh);
  ok($string, 'wrote to filehandle');
  my $expect = do {
    open(my $fh, '<', 'files/cube.stl') or die "cannot open cube.stl $!";
    local $/; <$fh>;
  };

  s/ */ /g for($string, $expect);
  s/\015?\012/\n/g for($string, $expect);
  is($string, $expect, 'string match');
}

done_testing();
# vim:ts=2:sw=2:et:sta
