Add catches_strings test to test throws char * typemap
This commit is contained in:
parent
034e2358f9
commit
4a29229bab
21 changed files with 286 additions and 0 deletions
13
Examples/test-suite/catches_strings.i
Normal file
13
Examples/test-suite/catches_strings.i
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
%module catches_strings
|
||||
|
||||
%include <std_string.i>
|
||||
|
||||
%catches(const char *) StringsThrower::charstring;
|
||||
|
||||
%inline %{
|
||||
struct StringsThrower {
|
||||
static void charstring() {
|
||||
throw "charstring message";
|
||||
}
|
||||
};
|
||||
%}
|
||||
|
|
@ -133,6 +133,7 @@ CPP_TEST_CASES += \
|
|||
bloody_hell \
|
||||
bools \
|
||||
catches \
|
||||
catches_strings \
|
||||
cast_operator \
|
||||
casts \
|
||||
char_binary \
|
||||
|
|
|
|||
20
Examples/test-suite/csharp/catches_strings_runme.cs
Normal file
20
Examples/test-suite/csharp/catches_strings_runme.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
using System;
|
||||
using catches_stringsNamespace;
|
||||
|
||||
public class catches_strings_runme {
|
||||
public static void Main()
|
||||
{
|
||||
{
|
||||
bool exception_thrown = false;
|
||||
try {
|
||||
StringsThrower.charstring();
|
||||
} catch (ApplicationException e) {
|
||||
if (!e.Message.Contains("charstring message"))
|
||||
throw new ApplicationException("incorrect exception message:" + e);
|
||||
exception_thrown = true;
|
||||
}
|
||||
if (!exception_thrown)
|
||||
throw new ApplicationException("Should have thrown an exception");
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Examples/test-suite/d/catches_strings_runme.1.d
Normal file
20
Examples/test-suite/d/catches_strings_runme.1.d
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
module catches_strings_runme;
|
||||
|
||||
import catches_strings.catches_strings;
|
||||
import catches_strings.StringsThrower;
|
||||
import std.algorithm;
|
||||
|
||||
void main() {
|
||||
{
|
||||
bool exception_thrown = false;
|
||||
try {
|
||||
StringsThrower.charstring();
|
||||
} catch (Exception e) {
|
||||
if (!canFind(e.msg, "charstring message"))
|
||||
throw new Exception("incorrect exception message:" ~ e.msg);
|
||||
exception_thrown = true;
|
||||
}
|
||||
if (!exception_thrown)
|
||||
throw new Exception("Should have thrown an exception");
|
||||
}
|
||||
}
|
||||
20
Examples/test-suite/d/catches_strings_runme.2.d
Normal file
20
Examples/test-suite/d/catches_strings_runme.2.d
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
module catches_strings_runme;
|
||||
|
||||
import catches_strings.catches_strings;
|
||||
import catches_strings.StringsThrower;
|
||||
import std.algorithm;
|
||||
|
||||
void main() {
|
||||
{
|
||||
bool exception_thrown = false;
|
||||
try {
|
||||
StringsThrower.charstring();
|
||||
} catch (Exception e) {
|
||||
if (!canFind(e.msg, "charstring message"))
|
||||
throw new Exception("incorrect exception message:" ~ e.msg);
|
||||
exception_thrown = true;
|
||||
}
|
||||
if (!exception_thrown)
|
||||
throw new Exception("Should have thrown an exception");
|
||||
}
|
||||
}
|
||||
17
Examples/test-suite/go/catches_strings_runme.go
Normal file
17
Examples/test-suite/go/catches_strings_runme.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package main
|
||||
|
||||
import "strings"
|
||||
import . "swigtests/catches_strings"
|
||||
|
||||
func main() {
|
||||
exception_thrown := false
|
||||
func() {
|
||||
defer func() {
|
||||
exception_thrown = strings.Index(recover().(string), "charstring message") == 0
|
||||
}()
|
||||
StringsThrowerCharstring()
|
||||
}()
|
||||
if !exception_thrown {
|
||||
panic(0)
|
||||
}
|
||||
}
|
||||
3
Examples/test-suite/guile/catches_strings_runme.scm
Normal file
3
Examples/test-suite/guile/catches_strings_runme.scm
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
(dynamic-call "scm_init_catches_strings_module" (dynamic-link "./libcatches_strings"))
|
||||
(load "testsuite.scm")
|
||||
(load "../schemerunme/catches_strings.scm")
|
||||
28
Examples/test-suite/java/catches_strings_runme.java
Normal file
28
Examples/test-suite/java/catches_strings_runme.java
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import catches_strings.*;
|
||||
|
||||
public class catches_strings_runme {
|
||||
static {
|
||||
try {
|
||||
System.loadLibrary("catches_strings");
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String argv[]) throws Throwable
|
||||
{
|
||||
{
|
||||
boolean exception_thrown = false;
|
||||
try {
|
||||
StringsThrower.charstring();
|
||||
} catch (RuntimeException e) {
|
||||
if (!e.getMessage().contains("charstring message"))
|
||||
throw new RuntimeException("incorrect exception message");
|
||||
exception_thrown = true;
|
||||
}
|
||||
if (!exception_thrown)
|
||||
throw new RuntimeException("Should have thrown an exception");
|
||||
}
|
||||
}
|
||||
}
|
||||
15
Examples/test-suite/javascript/catches_strings_runme.js
Normal file
15
Examples/test-suite/javascript/catches_strings_runme.js
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
var catches_strings = require("catches_strings");
|
||||
|
||||
exception_thrown = false;
|
||||
try {
|
||||
catches_strings.StringsThrower.charstring();
|
||||
} catch (e) {
|
||||
console.log(typeof(e))
|
||||
console.log(e.constructor.name)
|
||||
console.log(typeof(e.message))
|
||||
if (!e.message.includes("charstring message"))
|
||||
throw new Error("incorrect exception message " + e.message);
|
||||
exception_thrown = true;
|
||||
}
|
||||
if (!exception_thrown)
|
||||
throw new Error("Should have thrown an exception");
|
||||
10
Examples/test-suite/lua/catches_strings_runme.lua
Normal file
10
Examples/test-suite/lua/catches_strings_runme.lua
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
require("import") -- the import fn
|
||||
import("catches_strings") -- import code
|
||||
|
||||
-- catch "undefined" global variables
|
||||
local env = _ENV -- Lua 5.2
|
||||
if not env then env = getfenv () end -- Lua 5.1
|
||||
setmetatable(env, {__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
|
||||
|
||||
s, msg = pcall(function() catches_strings.StringsThrower.charstring() end)
|
||||
assert(s == false and msg:find("charstring message", 1, true))
|
||||
11
Examples/test-suite/mzscheme/catches_strings_runme.scm
Normal file
11
Examples/test-suite/mzscheme/catches_strings_runme.scm
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(load-extension "catches_strings.so")
|
||||
(require (lib "defmacro.ss"))
|
||||
|
||||
(define exception_thrown "no exception thrown for kin")
|
||||
(with-handlers ([exn:fail? (lambda (exn)
|
||||
(set! exception_thrown (exn-message exn)))])
|
||||
(StringsThrower-charstring))
|
||||
(unless (string-contains? exception_thrown "charstring message")
|
||||
(error (format "incorrect exception message: ~a" exception_thrown)))
|
||||
|
||||
(exit 0)
|
||||
8
Examples/test-suite/ocaml/catches_strings_runme.ml
Normal file
8
Examples/test-suite/ocaml/catches_strings_runme.ml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
open Swig
|
||||
open Catches_strings
|
||||
|
||||
let _ =
|
||||
try
|
||||
ignore (_StringsThrower_charstring (C_void)); assert false
|
||||
with Failure s ->
|
||||
assert (s = "charstring message")
|
||||
19
Examples/test-suite/octave/catches_strings_runme.m
Normal file
19
Examples/test-suite/octave/catches_strings_runme.m
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# do not dump Octave core
|
||||
if exist("crash_dumps_octave_core", "builtin")
|
||||
crash_dumps_octave_core(0);
|
||||
endif
|
||||
|
||||
catches_strings
|
||||
|
||||
exception_thrown = false;
|
||||
try
|
||||
StringsThrower.charstring();
|
||||
catch e
|
||||
if (isempty(strfind(e.message, "charstring message")))
|
||||
error("incorrect exception message: %s", e.message)
|
||||
endif
|
||||
exception_thrown = true;
|
||||
end_try_catch
|
||||
if (!exception_thrown)
|
||||
error("Should have thrown an exception");
|
||||
endif
|
||||
10
Examples/test-suite/perl5/catches_strings_runme.pl
Normal file
10
Examples/test-suite/perl5/catches_strings_runme.pl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
use Test::More tests => 3;
|
||||
BEGIN { use_ok('catches_strings') }
|
||||
require_ok('catches_strings');
|
||||
|
||||
eval {
|
||||
catches_strings::StringsThrower::charstring();
|
||||
};
|
||||
like($@, qr/\bcharstring message/, "Should have thrown an exception");
|
||||
14
Examples/test-suite/php/catches_strings_runme.php
Normal file
14
Examples/test-suite/php/catches_strings_runme.php
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
require "tests.php";
|
||||
|
||||
$exception_thrown = false;
|
||||
try {
|
||||
StringsThrower::charstring();
|
||||
} catch (Exception $e) {
|
||||
check::str_contains($e->getMessage(), "charstring message", "incorrect exception message: {$e->getMessage()}");
|
||||
$exception_thrown = true;
|
||||
}
|
||||
check::equal($exception_thrown, true, "Should have thrown an exception");
|
||||
|
||||
check::done();
|
||||
11
Examples/test-suite/python/catches_strings_runme.py
Normal file
11
Examples/test-suite/python/catches_strings_runme.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
from catches_strings import *
|
||||
|
||||
exception_thrown = False
|
||||
try:
|
||||
StringsThrower.charstring()
|
||||
except RuntimeError as e:
|
||||
if "charstring message" not in str(e):
|
||||
raise RuntimeError("incorrect exception message:" + str(e))
|
||||
exception_thrown = True
|
||||
if not exception_thrown:
|
||||
raise RuntimeError("Should have thrown an exception")
|
||||
15
Examples/test-suite/r/catches_strings_runme.R
Normal file
15
Examples/test-suite/r/catches_strings_runme.R
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
clargs <- commandArgs(trailing=TRUE)
|
||||
source(file.path(clargs[1], "unittest.R"))
|
||||
|
||||
dyn.load(paste("catches_strings", .Platform$dynlib.ext, sep=""))
|
||||
source("catches_strings.R")
|
||||
cacheMetaData(1)
|
||||
|
||||
exception_thrown = FALSE
|
||||
tryCatch({
|
||||
StringsThrower_charstring()
|
||||
}, error = function(e) {
|
||||
exception_thrown <<- grepl(e$message, "charstring message", fixed=TRUE)
|
||||
}
|
||||
)
|
||||
unittest(exception_thrown, TRUE)
|
||||
18
Examples/test-suite/ruby/catches_strings_runme.rb
Normal file
18
Examples/test-suite/ruby/catches_strings_runme.rb
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
require 'swig_assert'
|
||||
|
||||
require 'catches_strings'
|
||||
|
||||
exception_thrown = false
|
||||
begin
|
||||
Catches_strings::StringsThrower.charstring()
|
||||
rescue RuntimeError => e
|
||||
if (!e.to_s.include? "charstring message")
|
||||
raise RuntimeError, "incorrect exception message: #{e.to_s}"
|
||||
end
|
||||
exception_thrown = true
|
||||
end
|
||||
if (!exception_thrown)
|
||||
raise RuntimeError, "Should have thrown an exception"
|
||||
end
|
||||
5
Examples/test-suite/schemerunme/catches_strings.scm
Normal file
5
Examples/test-suite/schemerunme/catches_strings.scm
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(expect-throw 'swig-exception
|
||||
(StringsThrower-charstring))
|
||||
; TODO: check the exception message
|
||||
|
||||
(exit 0)
|
||||
10
Examples/test-suite/scilab/catches_strings_runme.sci
Normal file
10
Examples/test-suite/scilab/catches_strings_runme.sci
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
exec("swigtest.start", -1);
|
||||
|
||||
ierr = execstr("StringsThrower_charstring()", 'errcatch');
|
||||
checkequal(ierr, 20000, "wrong/no exception thrown")
|
||||
if (strstr(lasterror(), "charstring message") == '')
|
||||
printf("Should have thrown an exception")
|
||||
exit(1)
|
||||
end
|
||||
|
||||
exec("swigtest.quit", -1);
|
||||
18
Examples/test-suite/tcl/catches_strings_runme.tcl
Normal file
18
Examples/test-suite/tcl/catches_strings_runme.tcl
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
|
||||
if [ catch { load ./catches_strings[info sharedlibextension] catches_strings} err_msg ] {
|
||||
puts stderr "Could not load shared object:\n$err_msg"
|
||||
}
|
||||
|
||||
|
||||
set exception_thrown 0
|
||||
if [ catch {
|
||||
StringsThrower_charstring
|
||||
} e ] {
|
||||
if {[string first "charstring message" $e] == -1} {
|
||||
error "incorrect exception message: $e"
|
||||
}
|
||||
set exception_thrown 1
|
||||
}
|
||||
if {!$exception_thrown} {
|
||||
error "Should have thrown an exception"
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue