Error while executing perl application in locate.pl - windows

This is the error:
No 'new' for class 'Spec::Benchmark::bzip2401' in 'C:/Users/Tester/Documents/SpecINT2k6_WoT/benchspec/CPU2006/401.bzip2/Spec/object.pm'
point of error in locate.pl file:
my $class="Spec::Benchmark::${name}${num}";
if (!$class->can('new')) {
Log(0, "\nNo 'new' for class '$class' in '$pm'\n");
next;
}
here is the link to the whole locate.pl file http://ks.tier2.hep.manchester.ac.uk/Repositories/other-software/SPEC_CPU2006v1.1/bin/locate.pl
This is the object.pm file http://codepad.org/O196ykIq
I am getting this error while running Specint2006 suite, but this error is not related to the suite. Can anyone tell me what does !$class->can('new') do and why is it returning true here?
Thanks.

Can checks if the Class has the method. The return value is always the coderef. If the class dont know the method, the return value is undef.
The Class dont know the new method, so its false. But you call it with not
!$class->can('new')
Quote from HERE
Again, the same rule about having a valid invocand applies -- use an eval block or blessed if you need to be extra paranoid.

Related

How to quickly write debug output from within a PHP Spec test method

I have inherited some phpspec tests.
The test is testing the value of a method called "getFatalErrors" and reporting failure with:
expected [array:1], but got [array:1].
I would like to see the actual contents of the array.
I have tried to hack the phpspec test class by adding lines like:
<?php
namespace spec;
use MyClass;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class MyClassSpec extends ObjectBehavior
{
public function it_returns_a_path_problem($args,\XMLFileWrapperTest $testWrapper)
{
echo "foo";
...
var_dump(print_r($this->getFatalErrors()->getWrappedObject(), true));
...
fwrite(STDOUT, "foo");
print_r($this->getFatalErrors()->getWrappedObject(), true)
$this->display("foo");
}
}
--
But I can never get any output to show on my CLI output.
How can I make some arbitrary text appear in my test output so that I 'see' what is going on as I become more familiar with PHPSpec?
Try a different formatter.
When the formatter pretty is selected either in phpspec.yml using the line
formatter.name: pretty
or when executing the test runner with format flag
vendor/bin/phpspec run --format=pretty
then echo output is visible in the terminal.
Have you tried to tail the error log and in your function to add something like
error_log( print_r( $this->getFatalErrors()->getWrappedObject(), 1 ) ); or error_log( print_r( $this->getFatalErrors(), 1 ) ); ?
Usually, it works, as the output is written in your server error log and you can use the terminal or console to tail on that file and see in real time the result.
Just run phpspec with the -v flag, it will be more verbose.
According to the phpspec documentation on Matchers > Inline Matcher, it is possible...
to print a more verbose error message
to do this you can throw
FailureException
So, this implies that it is possible to throw FailureException to output custom messages from within your PHPSpec Example methods.
I tried this and it let me write the phrase "foo message" to my test output:
<?php
namespace spec;
use PhpSpec\ObjectBehavior;
[...]
use PhpSpec\Exception\Example\FailureException;
class MyClassSpec extends ObjectBehavior
{
public function it_tests_something()
{
[...]
throw new FailureException("foo message");
}
}

Rspec error: location has already been taken

I have following test case in rspec
let(:detail) { FactoryGirl.create(:detail) }
let(:url) do
"/detail/#{detail.detail_id}"\
"/user/#{detail.user.id}/details"
end
let(:location_header) do
detail_url(detail_id: detail.detail_id,
user: detail.user.id, location: detail.location)
end
before do
post url, params: params
end
context 'when valid location and value are passed, detail is created successfully.' do
let(:params) {{detail_app_id: detail.detail_app_id, location: detail.location, value: 'value'}}
it { expect(response.status).to eq(201) }
end
I am learning ruby rspec and i am getting an error "{\"error\":\"Validation failed: location has already been taken\"}"
when i change location value in params it passes but since i am creating factory girl for it i want to use that value rather passing different one.
Any idea why i am getting that error?
You need to go into one of your factories (I'm assuming detail) and assign its location properly (factories/detail.rb).
What's happening here is you're trying to create a new detail, which it attempts to save, but it fails the validation (I'm assuming you have a unique location validation on the detail model)
Check out http://www.rubydoc.info/gems/factory_bot/file/GETTING_STARTED.md, the sequences section, for how to give a unique attribute where required.

puppet stdlib 'member" function not working

Trying to use the member function of the puppet stdlib module:
effectively:
$myvariable = 'FOO'
then when using the member function:
member(['FOO','BAR'], $myvariable)
I keep getting the error message:
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Function 'member' must be the value of a statement at /etc/puppet/modules/mymodule/manifests/init.pp:###
Looking at the stdlib documentation for member, we see that member is an rvalue. This means in this context that you need to have its output assigned. This is what the error message of must be the value of a statement is hinting at. Note a helpful wikipedia article on l-values and r-values https://en.wikipedia.org/wiki/Value_(computer_science)#lrvalue.
Your code will work, for example, if you assign the output of member(['FOO','BAR'], $myvariable) to a variable or a resource attribute.
For example:
$myvariable = 'FOO'
$variable = member(['FOO','BAR'], $myvariable)
notify { $variable: }
will result in a notify 'true' during compilation.

How to use polymorphism to remove a switch statement which compares strings?

I am new to Ruby, so let me describe the context of my problem first:
I have a json as input which has the following key / value pair:
{
"service": "update"
}
The value has many different values for example: insert,delete etc.
Next there is a method x which handles the different requests:
def x(input)
case input[:service]
services = GenericService.new
when "update"
result = services.service(UpdateService.new,input)
when "insert"
result = services.service(InsertService.new,input)
when "delete"
result = services.service(DeleteService.new,input)
....
....
else
raise "Unknown service"
end
puts JSON.pretty_generate(result)
end
What is bothering me is that I still need to use a switch statement to check the String values (reminds me of 'instance of' ugh..). Is there a cleaner way (not need to use a switch)?
Finally I tried to search for an answer to my question and did not succeed, if however I missed it feel free to comment the related question.
Update: I was thinking to maybe cast the string to the related class name as follows: How do I create a class instance from a string name in ruby? and then call result = services.services(x.constantize.new,input) , then the class names ofcourse needs to match the input of the json.
You can try something like:
def x(input)
service_class_name = "#{input[:service].capitalize}Service"
service_class = Kernel.const_get(service_class_name)
service_class.new(input).process
end
In addition you might want to check if this is a valid Service class name at all.
I don't understand why you want to pass the service to GenericService this seems strange. let the service do it's job.
If you're trying to instatiate a class by it's name you're actually speaking about Reflection rather than Polymorphism.
In Ruby you can achieve this in this way:
byName = Object.const_get('YourClassName')
or if you are in a Rails app
byName= 'YourClassName'.constantize
Hope this helps
Just first thoughts, but you can do:
eval(services.service("#{input[:service].capitalize}Service.new, #{input})") if valid_service? input[:service]
def valid_service?
w%(delete update insert).include? input[:service]
end
As folks will no doubt shout, eval needs to be used with alot of care

RSpec - trying to stub a method that returns its own argument

I have a method which I'm trying to stub out in my unit test. The real method gets called with one argument (a string) and then sends out a text message. I need to stub out the method but return the string that gets passed in as an argument.
The code I have in my RSpec test is this:
allow(taxi_driver).to receive(:send_text).with(:string).and_return(string)
This returns:
NameError: undefined local variable or method 'string'
If I change the return argument to :string, I get the following error:
Please stub a default value first if message might be received with other args as well
I've tried googling and checking the relishapp.com site, but can't find the answer to something which appears quite simple and straightforward.
You can pass a block:
allow(taxi_driver).to receive(:send_text).with(kind_of(String)){|string| string }
expect(taxi_driver.send_text("123")).to eq("123")
My method is being called like this: send_text("the time now is #{Time.now}"). The string varies according to the time, thats why I need the mock to return the varying string. Perhaps its not within the scope of a mock to do this?
In such a case, I usually use Timecop gem in order to freeze system time. Here is a sample use case:
describe "#send_text" do
let(:taxi_driver) { TaxiDriver.new }
before do
Timecop.freeze(Time.local(2016, 1, 30, 12, 0, 0))
end
after do
Timecop.return
end
example do
expect(taxi_driver.send_text("the time now is #{Time.now}")).to eq \
"the time now is 2016-01-30 12:00:00 +0900"
end
end

Resources