Something like $AssertFunction=Abort[];Message[] - wolfram-mathematica

I'd like to customize the new Assert to print message as it usually does, but to also Abort[] all computation. Abort[] part is easy, but how do I generate the same message as the default?

How about this?
$AssertFunction = (Message[Assert::asrtf, #1]; Abort[]) &

Related

Wrap all the strings with single quotas

Let's assume that my template is like a following
string1=${obj.firstString}
string2=${obj.secondString}
number1=${obj.firstNumber}
I'm looking for some automatic way to wrap all my string parameters with single quotas? The expected output is
string1='A'
string2='B'
number1=42
I understand that I can write string1=${"'" + obj.firstString + "'"} , but maybe there is some more conventional way for this requirement...
Thanks a lot!
I would just do this:
string1='${obj.firstString}'
string2='${obj.secondString}'
number1=${obj.firstNumber}
It's a template language, so the basic idea is to make your program look similar to its own output.

Change the way an object is displayed in debugger/inspector variable-value table

I would like to know if there is a message I can override in Pharo so that my custom classes display more descriptive information in the inspector/debuger much like simple variable types do, like Integers or Strings. For instance:
Instead of that, I would like it to show a more custom and informative description consisting of its internal variales so as to have a tighter/tidier view of the variables instead of having to click on it and open another chart (therefore losing sight of the information on the previous chart). I know you can increase the amount of charts shown below, but that is not the point of the question. I would like to achieve something like this:
I have browsed the pharo forums and found nothing, I have also tried overriding over 30 methods hoping that one of them changed the output. Only the class message seemed to change the output, but I could only return an instance of Metaclass and besides messing with this message would break a lot of stuff. Finally I tried to reverse engineer the debugger and then the inspector to see at which point is the table constructed and what values are used or which messages are sent to build said values, but it was just too much for me, the callstack kept growing and I couldn't even scratch the surface.
Luckily, doing this in any Smalltalk is very easy. Types inherited from Object are expected to answer to the message printString, and ultimately printOn: aStream. Those messages are expected to give a description of the object. So, you should just override printOn: in your class (printString uses printOn:) and all the browsers and inspectors will automatically use it. There other possibilities in Pharo, if you want to provide more complex information in different tabs, but I think printOn: will suffice for you.
An example would be:
MyPoint>>printOn: aStream
aStream nextPut: ${.
x printOn: aStream.
aStream nextPutAll: ', '
y printOn: aStream.
aStream nextPut: $}
In Smalltalk, every time you observe something you don't like or understand, you ask the question: Which message is doing this?
In your case, the question would be: Which message creates the string a MyPoint that I see everywhere?
Next, to answer your question you need to find a good place for inserting a halt and then debug from there until you find the culprit. To do this just find the simplest expression that would reproduce the issue and debug it. In your case the right-click command in the Playground will do. So,
Write and select (MyPoint on: 14 and: -5) halt in a Playground.
Right-click and issue the Print it command (I'm assuming you already checked that this command produces the string 'a MyPoint').
Debug
Go over the evaluation of #DoIt, which answers the result
Continue this way alternating between Into and Over to make sure you follow the result to where it's being taken
Eventually you will reach the implementation of Object >> #printString. Bingo!
Now you can open a System Browser and take a look at this method, study how it's been implemented in different classes, etc. Your investigation should show you that the most basic message for printing is #printOn:. You may also want to take a look at other implementors so to better understand what people usually do. (Bear in mind that writing good #printOn:s is a minimalist art)
Overriding printOn: will work for simple cases where you want to just change description.
Pharo allows a lot more than that!
Due the extensible (moldable) nature of our inspector, you do not need to override a method to get your own visualisation of the object.
For example, look this array visualisation:
This is obtained adding this method to Collection:
gtInspectorItemsIn: composite
<gtInspectorPresentationOrder: 0>
^ composite fastList
title: 'Items';
display: [ self asOrderedCollection ];
beMultiple;
format: [ :each | GTObjectPrinter asTruncatedTextFrom: each ];
send: [ :result |
result
ifNil: [ nil ]
ifNotNil: [ result size = 1
ifTrue: [ result anyOne ]
ifFalse: [ self species withAll: result ]
]
]
if you browse for senders of gtInspectorPresentationOrder: you will see there are already a lot of special visualisations in the image.
You can take those as an example on how to create your own, adapted exactly to what you need :)

Proper way to assert text in Selenium Webdriver/RSpec

So im writing a framework in pure selenium-webdriver and am curious what the proper way to assert text exists (Such as in an alert message for instance on an invalid login for example). Specifically with RSpec.
I can think of two ways that comes to mind. Doing something like so:
text_to_check = driver.find_element(locator).text and then doing something like expect(text_to_check).to be("Bad Login text") The locator in this case would probably be xpath or css locator I guess? Although I feel like xpath would probably make more sense (Im not super familiar with xpath tbh though)
Use the driver.page_source() and then check against that....but that seems brittle if that text exists somewhere else on the page. Also it seems unnecessary to do that and pull in the whole page source to check what is essentially one element.
String expected = “abc.com;
String actualURL= “abc.com”;
Assert.assertEquals(expected, actualURL);
message – Message to be displayed in case of an Assertion Error.
condition – Condition against which the assertion needs to be applied.
Assert.assertTrue(“Assert True test message”, true);

Can anyone see what is wrong with my code?

My code is as follows:
a=msgbox("Do you like waffles",4+16,"Waffles")
Do While a=vbno
a
Loop
if a=vbyes then
b=msgbox("Do you like pancakes",4+16,"Pancakes")
Do While b=vbno
b
loop
if b=vbye then
c=msgbox("Do you like French toast",4+16,"French toast")
Do While c=vbno
c
Loop
else
d-msgbox("good",0+16,"YAY!")
end if
I know it is basic, but it comes up with the error message: "Error: Expected 'End'"
But as you can see 'end' is at the start of line 17, if it is something I haven't seen some where else in the code that might be causing this. I'm kind of new to this language and was putting things I knew how to do into a semi-useful paten.
I saved it as a .bat file and ran it using cmd.
Here is the original code posted in the question (as I'm writing this):
a=msgbox("Do you like waffles",4+16,"Waffles")
Do While a=vbno
a
Loop
if a=vbyes then
b=msgbox("Do you like pancakes",4+16,"Pancakes")
Do While b=vbno
b
loop
if b=vbye then
c=msgbox("Do you like French toast",4+16,"French toast")
Do While c=vbno
c
Loop
else
d-msgbox("good",0+16,"YAY!")
end if
A main error cause seems to be a notion that one can assign a definition to a variable. Well one can, but not in that way. Here is corrected code:
option explicit
dim answer
do
answer = msgbox("Do you like waffles",4+16,"Waffles")
if answer = vbYes then exit do
answer = msgbox("Do you like pancakes",4+16,"Pancakes")
if answer = vbYes then exit do
answer = msgbox("Do you like French toast",4+16,"French toast")
if answer = vbYes then exit do
loop
call msgbox( "good",0+16,"YAY!" )
There are however umpteen zillion ways to prepare food, and so also with code, plus, it's not clear if the above is the intention, but I guess it's pretty close.
At any rate, it's code that works and that you can build further on
Note that VBScript documentation is now only available as a CHM compiled help file, called "script56.chm".
The VBScript documentation is both available as a handy CHM compiled help file, called "script56.chm", as well as online in the MSDN library.
Note: you need to save the source code as a .vbs file.

What's a reasonable way to read an entire text file as a single string?

I am sure this is an easy one; I just couldn't find the answer immediately from Google.
I know I could do this (right?):
text = ""
File.open(path).each_line do |line|
text += line
end
# Do something with text
But that seems a bit excessive, doesn't it? Or is that the way one would do it in Ruby?
IO.read() is what you're looking for.
File is a subclass of IO, so you may as well just use:
text = File.read(path)
Can't get more intuitive than that.
What about IO.read()?
Edit: IO.read(), as an added bonus, closes the file for you.
First result I found when searching.
I wanted to change the mode, which doesn't seem possible with IO.read, unless I'm wrong?
Anyway, you can do this:
data = File.open(path,'rb',&:read)
It's also good for when you want to use any of the other options:
https://ruby-doc.org/core/IO.html#method-c-new

Resources