1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
# Copyright (c) 1997-2024
# Ewgenij Gawrilow, Michael Joswig, and the polymake team
# Technische Universität Berlin, Germany
# https://polymake.org
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version: http://www.gnu.org/licenses/gpl.txt.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#-------------------------------------------------------------------------------
use strict;
use namespaces;
use feature 'state';
use JSON;
package Polymake::Test::Tutorials;
use Polymake::Struct (
[ '@ISA' => 'Group' ],
[ 'new' => '$$$' ],
[ '$name' => '"tutorials.Tutorials"' ],
[ '$env' => '#1' ],
[ '$application' => 'undef' ],
[ '$pattern' => '#2' ],
[ '$extension' => 'undef' ],
[ '$base_dir' => '#3' ],
);
sub new {
my $self=&_new;
# pattern might be a path or a wildcard
push @{$self->subgroups},
map { new Tutorial($_,$self) }
( -f $self->pattern ?
($self->pattern) :
grep { -f } glob $self->base_dir."/".$self->pattern.".ipynb");
$self;
}
sub package_name {
"tutorials"
}
sub run_context { undef }
package Polymake::Test::Tutorial;
#one tutorial
use Polymake::Struct (
[ '@ISA' => 'Subgroup' ],
[ 'new' => '$$' ],
[ '$source_file' => '#1' ],
[ '$name' => 'undef' ],
[ '$group' => 'weak(#2)' ],
[ '$temp_dir' => 'undef' ],
);
sub new {
my $self=&_new;
my ($name) = $self->source_file =~ m/\/([^\/]*)\.ipynb$/g;
$self->name = $name;
$self;
}
sub create_testcases {
my ($self) = @_;
state $testpkg = "TutorialTestPkg000000";
++$testpkg;
my $json = do {
local $/;
open my $in, "<", $self->source_file
or die("Can't read ", $self->source_file, ": $!\n");
<$in>
};
my $p = decode_json($json);
my $name = $self->name;
my $app = $User::default_application;
my $body = Core::RuleFilter::warn_options() . <<"---";
package Polymake::$testpkg;
use application '$app'; declare +auto;
include "common::jupyter.rules";
# testing $name
---
$self->group->env->start_timers;
my $cnt = 0;
foreach (@{${$p}{"cells"}}) {
if (${$_}{"cell_type"} eq "code") { # iterate all code cells
# each cell might introduce independent local effects
# $Scope is localized in Group::run, no need to localize it here again
$body .= "\$Scope=new Scope();\n" if $cnt++;
foreach (@{${$_}{"source"}}) {
s{^ \s* application \s* ($anon_quoted_re) \s*;\s* $}
{use application $1;\n}xmg;
$body .= enforce_nl($_); # add a trailing newline after the command if not present (for ease of debugging)
}
}
}
$body .= <<"---";
delete \$Polymake::{"$testpkg\::"};
---
# always create a tempdir
$self->temp_dir //= new Polymake::Tempdir;
# copy input files if demo/files/<basename> exists
# should be opened with something like
# load("files/<basename>/someexample.poly")
if (-d $self->group->base_dir."/files/".$self->name) {
File::Path::make_path($self->temp_dir."/files/");
system("cp -RLp '".$self->group->base_dir."/files/".$self->name."/' '".$self->temp_dir."/files/'");
}
new TutorialCase("body", $body, $self->source_file);
}
package Polymake::Test::TutorialCase;
use Polymake::Struct (
[ '@ISA' => 'Output' ],
[ 'new' => '$$$' ],
[ '$name' => '#1' ],
[ '$body' => '#2' ],
[ '$source_file' => '#3' ],
);
sub run_code {
my ($self) = @_;
local $disable_viewers = true;
my $tdir = new TempChangeDir($self->subgroup->temp_dir);
# files created in tutorials via save(), save_data() etc. must be automatically destroyed
# script() must be exempt from this transformation because it refers to existing scripts
local ref *replace_special_paths = sub {
if ((caller(1))[3] ne "Polymake::User::script") {
foreach (@_) {
s{^~(?=/|$)}{ $self->subgroup->temp_dir }e
or
s{^(?=[^/])}{ $self->subgroup->temp_dir."/" }e;
}
}
};
eval $self->body;
if ($@) {
$self->gotten_error = neutralized_ERROR();
$@ = "";
}
}
sub execute {
my ($self)=@_;
if (length($self->gotten_error)) {
$self->fail_log="expected: regular return\n".
" got: EXCEPTION: ".$self->gotten_error;
return 0;
}
1
}
1
|